我有两个列表要合并到一个数据框中。列表遵循此基本逻辑。 list1包含国家/地区名称,list2包含从list1中的每个值中减去的值。
<p>This is paragraph one</p>
<p>This is paragraph two</p>
<p>This is paragraph three</p>
<p>This is paragraph four</p>
我想要的结果是这样的:
list1<-list("A", "C", "B")
list2<-list(c("la la", "po sdfejn kfgndñflgn"), "characther(0)", c("4 5","baby", "yeah")) # note that characther(0) means that there is no data
我经常使用在此页中看到的代码,例如:output <- data.frame( V1 = c(rep("A",2), "C", rep("B",3)), V2 = c("la la", "po sdfejn kfgndñflgn", "characther(0)", "4 5", "baby", "yeah"))
。此代码适用于示例。但是,当我对正在处理的大型列表使用此公式时,会出现以下错误消息:
solution1 <- do.call(rbind, Map(data.frame, a = list1, b= list2))
我已尝试Error in (function (..., row.names = NULL, check.rows = FALSE, check.names =
TRUE, : arguments imply differing number of rows: 1, 0
,但是它不起作用。如果两个列表相同,那么问题的根源到底在哪里,如何解决才能获得所需的输出?
答案 0 :(得分:1)
使用tidyverse
的解决方案。 dat
是最终输出。
library(tidyverse)
dat <- map2_df(list1, list2, ~data_frame(V1 = .x, V2 = .y))
dat
# # A tibble: 6 x 2
# V1 V2
# <chr> <chr>
# 1 A la la
# 2 A po sdfejn kfgndñflgn
# 3 C characther(0)
# 4 B 4 5
# 5 B baby
# 6 B yeah