[英]create list of data frames from two lists of vectors via mapply
I expect a list of two data.frame instead of list of 4 vectors. 我希望有两个data.frame列表,而不是4个向量列表。 what did i do wrong?
我做错了什么?
list1 <- list(1:3, 5:6)
list2 <- list(7:9, 10:11)
result <- mapply((function(x, y) data.frame(id = x, value = y)),
list1, list2)
str(result)
# List of 4
# $ : int [1:3] 1 2 3
# $ : int [1:3] 7 8 9
# $ : int [1:2] 5 6
# $ : int [1:2] 10 11
# - attr(*, "dim")= int [1:2] 2 2
# - attr(*, "dimnames")=List of 2
# ..$ : chr [1:2] "id" "value"
# ..$ : NULL
You can use SIMPLIFY=FALSE
in mapply
to conserve the list
structure 您可以在
mapply
使用SIMPLIFY=FALSE
来保存list
结构
mapply(function(x, y) data.frame(id = x, value = y),
list1, list2, SIMPLIFY=FALSE)
Or use Map
或使用
Map
Map(function(x,y) data.frame(id=x, value=y), list1, list2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.