2017-01-23 131 views
0

我有兩個data.frames firstdf和secondf,(示例數據如下。)我試圖創建一個函數,可以輸出結果,如下面的ExampleList數據。我想創建一個列表列表,它從firstdf獲取第一行條目,並將它們的值放入exampleList中的thing和test字段中,然後從seconddf中的otherthing字段獲取前3個值,並將它們連接在一起,並保存他們在exampleList中的其他字段中,然後移到firstdf中的下一行和seconddf中的下3個行中。循環對我來說有點棘手,所以提示是絕對讚賞的。函數來創建從兩個data.frames列表的列表

data: 

dput(firstdf) 
structure(list(thing = structure(1:3, .Label = c("thing1", "thing2", 
"thing3"), class = "factor"), test = structure(1:3, .Label = c("test1", 
"test2", "test3"), class = "factor")), .Names = c("thing", "test" 
), row.names = c(NA, -3L), class = "data.frame") 

dput(seconddf) 
    structure(list(otherthing = structure(c(4L, 5L, 6L, 7L, 8L, 9L, 
    1L, 2L, 3L), .Label = c("thing10", "thing11", "thing12", "thing4", 
    "thing5", "thing6", "thing7", "thing8", "thing9"), class = "factor"), 
     other = structure(c(9L, 6L, 7L, 2L, 3L, 1L, 8L, 4L, 5L), .Label = c("fads", 
     "oiu", "qwer", "rewa", "rewq", "sfas", "sfwg", "tre", "xdfs" 
     ), class = "factor")), .Names = c("otherthing", "other"), row.names = c(NA, 
    -9L), class = "data.frame") 

輸出:

dput(ExampleList) 
list(structure(list(thing = "thing1", test = "test1", otherthing = c("thing4", 
"thing5", "thing6")), .Names = c("thing", "test", "otherthing" 
)), structure(list(thing = "thing2", test = "test2", otherthing = c("thing7", 
"thing8", "thing9")), .Names = c("thing", "test", "otherthing" 
)), structure(list(thing = "thing3", test = "test3", otherthing = c("thing10", 
"thing11", "thing12")), .Names = c("thing", "test", "otherthing" 
))) 
[[1]] 
[[1]]$thing 
[1] "thing1" 

[[1]]$test 
[1] "test1" 

[[1]]$otherthing 
[1] "thing4" "thing5" "thing6" 


[[2]] 
[[2]]$thing 
[1] "thing2" 

[[2]]$test 
[1] "test2" 

[[2]]$otherthing 
[1] "thing7" "thing8" "thing9" 


[[3]] 
[[3]]$thing 
[1] "thing3" 

[[3]]$test 
[1] "test3" 

[[3]]$otherthing 
[1] "thing10" "thing11" "thing12" 
+0

的可能的複製http://stackoverflow.com/questions/26177565/converting-nested-list-to-dataframe – akrun

+0

@akrun我願意把這個作爲重複,但我們需要一個更好的目標imho – Jaap

+0

我說盡可能,而不是肯定 – akrun

回答

0

你可以使用Map,多元版lapply(與splitotherthing)。第一個參數是施加於將被並行迭代上的多個參數的函數,所以

ExampleList <- Map(list, 
    thing = as.character(firstdf$thing), 
    test = as.character(firstdf$test), 
    otherthing = split(as.character(seconddf[[1]]), rep(1:3, each = 3))) 

str(ExampleList) 

## List of 3 
## $ thing1:List of 3 
## ..$ thing  : chr "thing1" 
## ..$ test  : chr "test1" 
## ..$ otherthing: chr [1:3] "thing4" "thing5" "thing6" 
## $ thing2:List of 3 
## ..$ thing  : chr "thing2" 
## ..$ test  : chr "test2" 
## ..$ otherthing: chr [1:3] "thing7" "thing8" "thing9" 
## $ thing3:List of 3 
## ..$ thing  : chr "thing3" 
## ..$ test  : chr "test3" 
## ..$ otherthing: chr [1:3] "thing10" "thing11" "thing12" 
+0

謝謝你,你的建議與我的樣本數據非常吻合。但是,我的真實數據secondddf數據更長,並且當我嘗試您的建議時出現以下錯誤。有沒有辦法修改你的代碼來處理更長的seconddf?錯誤:警告信息: 在split.default(as.character(seconddf [[1]]),rep(長度(firstdf $ thing),: 數據長度不是分割變量的倍數 – user3476463

+0

檢出'?split' 。從這個例子中,我假設你想要三分裂,但如果它對於你的真實數據是不同的,你需要構造一個向量來拆分,這個向量與要拆分的向量長度相同,每個拆分的唯一值我想結束。 – alistaire