问题描述
ls(pattern =")
函数对我来说非常有用,因为我的对象列表似乎在不断增长.我很好奇这个功能是否会更有用.
The ls(pattern="")
function is very useful for me, since my list of objects seem to keep growing and growing. I am curious if this feature can be more useful.
例如,假设我有4个对象,
For example, let's say i have 4 objects,
a.c<-1
b.c<-2
c.c<-3
d.c<-4
现在我使用有用的 ls(pattern =")
函数
ls(pattern=".c")
现在我尝试列出一个清单
Now i try to make a list
list(ls(patter=".c"))
但是它并没有给我任何有用的信息("a.c""b.c""c.c""d.c"
).我想要这两个输出之一
But it doesn't give me anything useful( "a.c" "b.c" "c.c" "d.c"
). I want either of these two outputs
1,2,3,4
OR
a.c, b.c, c.c, d.c
推荐答案
几个问题:
1) ".c"
中的 .
被忽略,你需要转义"它:
1) The .
in ".c"
gets ignored, you need to "escape" it:
ls(pattern="\\.c")
否则,它将返回所有带有 c
的对象,而不管其是否带有句点.
Otherwise it will return all objects with c
regardless of having a period.
2) ls
返回对象的名称作为字符.要基于对象的名称获取值,您需要功能 get
:
2) ls
returns names of objects as character. To get the value of an object based on its name you need the function get
:
lapply(ls(pattern="\\.c"), get)
3)正如joran在评论中所提到的,最好将对象彼此关联在列表中:
3) As joran mentioned in the comments, it's much better to keep objects associated with each other in lists:
List.c = list(a.c=1, b.c=2, c.c=3, d.c=4)
这篇关于从ls(pattern =“"“)列出列表[R]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!