[英]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.files
的pattern
参数的list.files
, list.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.