2013-07-18 147 views
-1

我想改變dataframes的列表列出清單,以便能夠添加和刪除元素從dataframes.I想要的東西,如落得:如何將數據框列表轉換爲列表列表?

例子:

> list <- list(list("p"=runif(5), 
        "n"=round(1000*rnorm(5,5,2))), 
       list("p"=runif(10), 
        "n"=round(1000*rnorm(10,5,2))), 
       list("p"=runif(15), 
        "n"=round(1000*rnorm(15,5,2))) 
) 
> class(list[[1]]) 
[1] "list" 
+1

你做的理由沒有解釋清楚。 – Roland

回答

1

由於你的問題是不夠清楚,我,我想你有這樣的事情:

List <- list(data.frame(a=1:4, b=4:1), 
     data.frame(A=2:6, B=6:2)) 

這是data.frames的列表,你wnat它是一個列表:

unlist(lapply(List, as.list), recursive=FALSE) 

或列表

lapply(List, as.list) 
1

一個data.frame列表實際上是一個列表。你只需要刪除班級。

mylist <- list(df1=data.frame(a=1:10, b=11:20),df2=data.frame(a=c("a","b"), b=c("c","d"))) 
str(mylist) 
# List of 2 
# $ df1:'data.frame': 10 obs. of 2 variables: 
# ..$ a: int [1:10] 1 2 3 4 5 6 7 8 9 10 
# ..$ b: int [1:10] 11 12 13 14 15 16 17 18 19 20 
# $ df2:'data.frame': 2 obs. of 2 variables: 
# ..$ a: Factor w/ 2 levels "a","b": 1 2 
# ..$ b: Factor w/ 2 levels "c","d": 1 2 

mylist2 <- lapply(mylist, unclass) 
str(mylist2) 
# List of 2 
# $ df1:List of 2 
# ..$ a: int [1:10] 1 2 3 4 5 6 7 8 9 10 
# ..$ b: int [1:10] 11 12 13 14 15 16 17 18 19 20 
# ..- attr(*, "row.names")= int [1:10] 1 2 3 4 5 6 7 8 9 10 
# $ df2:List of 2 
# ..$ a: Factor w/ 2 levels "a","b": 1 2 
# ..$ b: Factor w/ 2 levels "c","d": 1 2 
# ..- attr(*, "row.names")= int [1:2] 1 2