简体   繁体   English

获取R中的目录中的文件号?

[英]Get files number in a dir in R?

in shell ,to make a dir: 在shell中,制作一个目录:

mkdir  /home/test

then ,to create a file named ".test" in the "/home/test" 然后,在“ / home / test”中创建一个名为“ .test”的文件

a=list.files(path = "/home/test",include.dirs = FALSE)
a
character(0)
a=list.files(path = "/home/test",include.dirs = TRUE)
a
character(0)
a=list.files(path = "/home/test/",include.dirs = TRUE)
a
character(0)
list.files(path = '/home/test', all.files=TRUE,inclued.dirs=FALSE)
[1] "."     ".."    ".test"
a=list.files(path = '/home/test', all.files=TRUE)
length(a)
[1] 3

how can i get length(a) = 1 using regular expression parameters pattern= in list.files to prune . 我如何使用list.files正则表达式参数pattern=来获取length(a) = 1以修剪. and .. ..

Use all.files=TRUE to show all file names including hidden files. 使用all.files=TRUE可以显示所有文件名,包括隐藏文件。

list.files(path = '/home/test', all.files=TRUE)

To answer your edit, one way would be to use a negative number with tail 要回答您的修改,一种方法是使用带tail号的负数

tail(list.files(path = '/home/test', all.files=TRUE), -2)

Using only the pattern argument: 仅使用pattern参数:

list.files(path='/home/test', all.files=TRUE, pattern="^[^\\.]|\\.[^\\.]")

The pattern says "anything that starts with something other than a dot or anything that starts with a dot followed by anything other than a dot." pattern表示“以点以外的任何内容或以点以外的其他任何内容开头的内容。”


Although it breaks your requirement to use the pattern argument of list.files , I would actually probably wrap grep around list.statements in this case. 尽管这违反了使用list.filespattern参数的list.fileslist.statements在这种情况下,我实际上可能会将grep包装在list.statements周围。

grep("^\\.*\\.$", list.files(path='/home/test', all.files=TRUE), 
     invert=TRUE, value=TRUE)

The above will find any file names that only contain dots, then return everything else. 上面的代码将找到仅包含点的任何文件名,然后返回其他所有文件名。 invert=TRUE means "find the names that do not match", and value=TRUE means "return the names instead of their location." invert=TRUE表示“查找不匹配的名称”,而value=TRUE表示“返回名称而不是其位置”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM