请考虑以下列表。
lst <- list()
lst[[1]] <- data.frame(category = c(1:10), freq = rnorm(10))
lst[[2]] <- data.frame(category = seq(4,26,2), freq = rnorm(12))
lst[[3]] <- data.frame(category = 2:8, freq = rnorm(7))
如果这个列表中的数据框具有不同的维度和级别,如何合并此类别中的数据框,生成如下所示的data.frame?
category freq.1 freq.2 freq.3
1 1.2496154 NA NA
2 -1.3537722 NA -1.3257535
3 -0.3976305 NA 0.5761957
4 0.7721428 -0.2360636 -1.0953490
5 0.7653834 NA 0.9037617
6 0.2192559 -0.2543082 -1.6640824
7 0.3400192 NA -1.2961707
8 -0.8457081 -0.7431021 0.9321560
9 -1.2132615 NA NA
10 1.8387899 0.8342264 NA
12 NA -0.5409164 NA
14 NA 0.1638546 NA
16 NA 0.6420546 NA
18 NA -0.5110151 NA
20 NA -0.8262690 NA
22 NA 1.2406462 NA
24 NA -1.1497992 NA
26 NA -0.4338807 NA
答案 0 :(得分:7)
Reduce(function(x, y) merge(x, y, by = 'category', all = TRUE), lst)