2011-09-08 80 views
2

給出列表的列表本身,是否有一種優雅的方式將原始文件轉換爲對待?我使用簡單的值,如1,2,3,但值可能是數據幀或任何。我們的目標不是去重複每個獨特命名的內容,只是通過合併內容來重複名稱。按重複名稱合併列表列表中的內容

original = structure(list(name1 = structure(list(one = 1, two = 2, three = 3), .Names = c("one", 
"two", "three")), name2 = structure(list(a = 9), .Names = "a"), 
    name1 = structure(list(four = 4, five = 5, six = 6), .Names = c("four", 
    "five", "six")), name2 = structure(list(b = 8), .Names = "b")), .Names = c("name1", 
"name2", "name1", "name2")) 

treated = structure(list(name1 = structure(list(one = 1, two = 2, three = 3, 
    four = 4, five = 5, six = 6), .Names = c("one", "two", "three", 
"four", "five", "six")), name2 = structure(list(a = 9, b = 8), .Names = c("a", 
"b"))), .Names = c("name1", "name2")) 
+0

modifyList我想(在我的手機上) – hadley

回答

5

下面是使用plyr溶液。

require(plyr) 
lnames = names(original) 
ulnames = unique(lnames) 
treated = plyr::llply(ulnames, function(x) original[lnames == x]) 
treated = llply(treated, unlist, recursive = F) 
names(treated) = ulnames