开发者

R! remove element from list which start from specific letters

I create a list of files:

folder_GLDAS=dir(foldery[numeryfolderow],pattern="_OBC.asc",recursive=F,full.names=T)

Unfortunately there is one additional object which i would like to remove (file name begin with "NOWY" - NOWYevirainf_OBC.asc).

How can I find index of this el开发者_如何学Goement on list to remove it by typing: folder_GLDAS<=folder_GLDAS[-to_remove] ??


Filter by using a regular expression.

folder_GLDAS <- folder_GLDAS[!grepl("^NOWY", folder_GLDAS)]

(You can also swap grepl for str_detect in stringr.)


Assuming that your list is one-dimensional, something like this should work:

*folder_GLDAS<-*folder_GLDAS[substr(*folder_GLDAS,1,4)!='NOWY']


You can actually make a (rather complex) PERL regex pattern that matches all names that end in "_OBC.asc" but DO NOT start with "NOWY": "^(?!NOWY).*_OBC\\.asc$"

Unfortunately the PERL syntax is not recognized by dir. But you could do it with grep like this:

folder_GLDAS <- dir(foldery[numeryfolderow],recursive=F,full.names=T)
folder_GLDAS <- grep(folder_GLDAS, pattern="^(?!NOWY).*_OBC\\.asc$", perl=T, value=T)

Also note that the "." in "_OBC.asc" needs to be escaped - otherwise you'll match for example "_OBCXasc" as well).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜