List files in R that do NOT match a pattern

后端 未结 2 2029
感情败类
感情败类 2021-01-03 20:25

R has a function to list files in a directory, which is list.files(). It comes with the optional parameter pattern= to list only files

2条回答
  •  梦毁少年i
    2021-01-03 20:32

    I belive you will have to do it yourself, as list.files does not support Perl regex (so you couldn't do something like pattern=^(?!new_)).

    i.e. list all files then filter them with grep:

    grep(list.files(path="data"), pattern='new_', invert=TRUE, value=TRUE)
    

    The grep(...) does the pattern matching; invert=TRUE inverts the match; value=TRUE returns the values of the matches (i.e. the filenames) rather than the indices of the matches.

提交回复
热议问题