Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/71.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 列表中的向量名称_R_Lapply - Fatal编程技术网

R 列表中的向量名称

R 列表中的向量名称,r,lapply,R,Lapply,我有相同长度的向量列表,我正在寻找一些简单的方法来命名向量的元素。我可以这样使用for循环: myList <- list(c(1,2,3), c(4,5,6), c(7,8,9)) for (i in 1:3){ names(myList[[i]]) <- c("a", "b", "c") } myList您可以尝试以下方法: lapply(myList, function(x) { names(x) <- c('a', 'b', 'c'); x}) lappy(myLi

我有相同长度的向量列表,我正在寻找一些简单的方法来命名向量的元素。我可以这样使用for循环:

myList <- list(c(1,2,3), c(4,5,6), c(7,8,9))
for (i in 1:3){ names(myList[[i]]) <- c("a", "b", "c") }
myList您可以尝试以下方法:

lapply(myList, function(x) { names(x) <- c('a', 'b', 'c'); x})
lappy(myList,function(x){names(x)我们可以试试

lapply(myList, setNames, letters[1:3])

也许
lappy(myList,`names)如果向量不仅长度相同,而且类型也相同,那么列表可能不是最好的开始结构……谢谢你,大卫,这一个有效!
lapply(myList, setNames, letters[1:3])