2015-10-20 74 views
4

我有數據幀的列表,看起來像這樣:從數據列表合併數據幀幀

ls[[1]] 
[[1]] 

month year oracle 
    1 2004 356.0000 
    2 2004 390.0000 
    3 2004 394.4286 
    4 2004 391.8571 
ls[[2]] 
[[2]] 
month year microsoft 
    1 2004 339.0000 
    2 2004 357.7143 
    3 2004 347.1429 
    4 2004 333.2857 

如何創建看起來像這樣的一個數據幀:

month year oracle microsoft 
    1 2004 356.0000 339.0000 
    2 2004 390.0000 357.7143 
    3 2004 394.4286 347.1429 
    4 2004 391.8571 333.2857 

回答

5

我們也可以使用Reduce

Reduce(function(...) merge(..., by = c('month', 'year')), lst) 

使用@夏侯的例子,如果值是不一樣的,使用的選項爲。

Reduce(function(...) merge(..., by = c('month', 'year'), all=TRUE), ls) 
#  month year oracle microsoft google 
#1  1 2004 356.0000  NA  NA 
#2  2 2004 390.0000 339.0000  NA 
#3  3 2004 394.4286 357.7143 390.0000 
#4  4 2004 391.8571 347.1429 391.8571 
#5  5 2004  NA 333.2857 357.7143 
#6  6 2004  NA  NA 333.2857 
+1

哎喲,我忘了'merge'選項 – Jaap

1

你也可以做do.call()如下...

do.call(merge, ls)

+0

此,如果有2組以上列表中的元素是行不通 – akrun

+1

我意思是假設你有3個列表元素,它會給出錯誤。 – akrun

+0

是的,所採取的措施......將爲以上案件工作 – Gaurav

4

運用@ akrun的答案Reduce/merge代碼將工作的偉大,如果爲monthyear列中的值是每個數據幀都相同。然而,當他們是不一樣的(例如數據在這個答案的末尾)

Reduce(function(...) merge(..., by = c('month', 'year')), ls) 

將返回僅在每個數據幀是常見的行:

month year oracle microsoft google 
1  3 2004 394.4286 357.7143 390.0000 
2  4 2004 391.8571 347.1429 391.8571 

在這種情況下,你可以要麼使用all=TRUE(如圖@akrun),或者當要包括所有行/觀測使用full_joindplyr包作爲替代:

library(dplyr) 
Reduce(function(...) full_join(..., by = c('month', 'year')), ls) 
# or just: 
Reduce(full_join, ls) 

這將導致:

month year oracle microsoft google 
1  1 2004 356.0000  NA  NA 
2  2 2004 390.0000 339.0000  NA 
3  3 2004 394.4286 357.7143 390.0000 
4  4 2004 391.8571 347.1429 391.8571 
5  5 2004  NA 333.2857 357.7143 
6  6 2004  NA  NA 333.2857 

使用的數據

ls <- list(structure(list(month = 1:4, year = c(2004L, 2004L, 2004L, 2004L), oracle = c(356, 390, 394.4286, 391.8571)), .Names = c("month", "year", "oracle"), class = "data.frame", row.names = c(NA, -4L)), 
      structure(list(month = 2:5, year = c(2004L, 2004L, 2004L, 2004L), microsoft = c(339, 357.7143, 347.1429, 333.2857)), .Names = c("month", "year", "microsoft"), class = "data.frame", row.names = c(NA,-4L)), 
      structure(list(month = 3:6, year = c(2004L, 2004L, 2004L, 2004L), google = c(390, 391.8571, 357.7143, 333.2857)), .Names = c("month", "year", "google"), class = "data.frame", row.names = c(NA,-4L)))