Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R sub使用do.call、lappy或mappy从向量列表中选择?_R_Lapply_Mapply_Do.call - Fatal编程技术网

R sub使用do.call、lappy或mappy从向量列表中选择?

R sub使用do.call、lappy或mappy从向量列表中选择?,r,lapply,mapply,do.call,R,Lapply,Mapply,Do.call,我有以下数据x是向量列表,索引是索引列表 x = list(c("a", "b", "c", "a"), c("b", "x", "a", "c")) indices = list(c(1, 2), c(3, 4)) 我要做的是逐步遍历列表x中表示的每个向量,然后根据索引向量从该向量中进行子选择。因此,预期的结果是 a,b a,c 我用mapply > mapply('[',x,indices) [,1] [,2] [1,] "a" "a" [2,] "b" "c"

我有以下数据
x
是向量列表,
索引
是索引列表

x = list(c("a", "b", "c", "a"), c("b", "x", "a", "c"))
indices = list(c(1, 2), c(3, 4))
我要做的是逐步遍历列表
x
中表示的每个向量,然后根据
索引
向量从该向量中进行子选择。因此,预期的结果是

a,b
a,c
我用
mapply

> mapply('[',x,indices)
     [,1] [,2]
[1,] "a"  "a" 
[2,] "b"  "c" 

但这不是我想要的。有什么建议吗?提前谢谢。

你很接近了。索引后,可以将元素折叠为单个字符串。在这里,我使用一个包装器(
toString
)进行
粘贴(,collapse=',)

f1
 f1 <- function(x,y) toString(x[y])
 mapply(f1,x,indices)
 #[1] "a, b" "a, c"