通过数据帧中的字符向量对我的R列表进行分组 [英] Subset my R list by character vector from dataframe
问题描述
我有一个r对象"d",它是一个列表.我希望引用此列表的子集的数据框作为函数"myfunction"的变量.该函数将使用rslurm调用数千次,每次使用d的不同子集.
I have an r object, 'd' that is a list. I want a dataframe of references to subsets of this list to make as variables for a function, 'myfunction'. This function will be called thousands of times using rslurm each using a different subset of d.
示例:d [['1']] [[3]]引用列表中的数据矩阵.
example: d[['1']][[3]] references a data matrix within the list.
myfunction(d[['1']][[3]])
工作正常,但我希望能够从数据框中调用这些子集.
works fine, but I want to be able to call these subsets from a dataframe.
我希望能够有一个包含所有子集引用的数据框"ds".
I want to be able to have a dataframe, 'ds' containing all of my subset references.
>ds
d
1 d[['1']][[3]]
2 d[['1']][[4]]
>myfunction(get(ds[1,1]))
Error in get(ds[1, 1]) : object 'd[['1']][[3]]' not found
是否有类似"get"之类的东西可以让我调用对象的子集d?或者我可以在"myfunction"中添加一些内容,以澄清该字符串引用了d的子集吗?
Is there something like 'get' that will let me call a subset of my object, d? Or something I can put in 'myfunction' that will clarify that this string references a subset of d?
推荐答案
我通过将myfunction更改为采用两个变量c和w并在更新函数的第一行中使用方括号表示法定义d来解决了此问题.我的ds现在有两个变量c和w,变量c定义为as.character,并且可以正常工作!
I solved this by changing myfunction to take two variables, c and w, and defining d using bracket notation in the first line of the updated function. My ds now has two variables, c and w, with variable c defined as as.character and it works!
myfunction(c,w) {
d<-d[[c]][[w]]
....rest of function}
>ds
c w
1 1 3
2 1 4
>test <- myfunction(ds[1,1],ds[1,2])
这篇关于通过数据帧中的字符向量对我的R列表进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!