convert list of Rdata into dataframe in R

I have 1000 lists saved as Rdata all saved in one directory. Each list has the same name, has 5 items and looks like this:

list.hv_Amono_volume

[[1]]
[1] 1.059246

[[2]]
[1] 1.047688

[[3]]
[1] 10.70799

[[4]]
[1] 10.30472

[[5]]
[1] 2.53379

When first storing the files in my directory I had given each file a unique name ie.: 1_list.hv_Amono_volume_rand.Rdata,

2_list.hv_Amono_volume_rand.Rdata,

3_list.hv_Amono_volume_rand.Rdata ... 1000_list.hv_Amono_volume_rand.Rdata

I have loaded all 1000 Rdata files as follows:

 temp =list.files(path="mydirectory",pattern="*_list.hv_Amono_volume_rand.Rdata")

but now I don't know how to extract the data from this list, obviously 'temp' gives me this

[1] "1_list.hv_Amono_volume_rand.Rdata"     "10_list.hv_Amono_volume_rand.Rdata"     
[3] "100_list.hv_Amono_volume_rand.Rdata"  "1000_list.hv_Amono_volume_rand.Rdata"
[5] "101_list.hv_Amono_volume_rand.Rdata"  "102_list.hv_Amono_volume_rand.Rdata" 
[7] "103_list.hv_Amono_volume_rand.Rdata"  "104_list.hv_Amono_volume_rand.Rdata" 
[9] "105_list.hv_Amono_volume_rand.Rdata"  "106_list.hv_Amono_volume_rand.Rdata" 

[11] "107_list.hv_Amono_volume_rand.Rdata" "108_list.hv_Amono_volume_rand.Rdata" [13] "109_list.hv_Amono_volume_rand.Rdata" "11_list.hv_Amono_volume_rand.Rdata"
[15] "110_list.hv_Amono_volume_rand.Rdata" "111_list.hv_Amono_volume_rand.Rdata" ...

So I now need to know how to extract the data from the list and to bind it to a dataframe with 1000 columns which looks like this:

1          2         ... 1000
1.059246   1.044808      1.046917
1.047688   1.046857      1.036242
10.70799   10.70204      10.0781
10.30472   9.319236      10.29681
2.53379    2.430255      2.482879

I have tried playing around with this named.list <- lapply(temp,load)

and this

sapply(Amono_list.hv,  function(x) load(x, .GlobalEnv), USE.NAMES=FALSE)

but I am honestly very lost and not getting anywhere so your help would be extremely appreciated.


#This may work 
do.call(cbind,mget(paste0(1:1000,"_list.hv_Amono_volume_rand")))

#Edit: as per comments

例:

x1<-as.list(1:3)
x2<-as.list(4:6)
save(x1,file="mydata1.RData") 
save(x2,file="mydata2.RData") 
tem<-list.files(pattern="*.RData")
str(tem)
 chr [1:2] "mydata1.RData" "mydata2.RData"
kk<-lapply(tem,load)
List of 2
 $ : chr "x1"
 $ : chr "x2"

do.call(cbind,lapply(1:2,function(i)get(kk[[i]])))
     [,1] [,2]
[1,] 1    4   
[2,] 2    5   
[3,] 3    6  

This is untested. The idea is that when you load() a file, there are R objects that get loaded. For this example, I'm assuming each file only contains a single R object. In the getList() function, we load the file into a temporary environment, read it, find the name, and return the object (which is hopefully a list). Major assumption here is that there's only one object per file and it's a list. Then use sapply like you had tried...

temp <-list.files(path="mydirectory",pattern="*_list.hv_Amono_volume_rand.Rdata")
e <- new.env()
getList <- function(filename){
  rm(list = ls(all = TRUE), envir=e) 
  load(filename, envir=e)
  # get name of thing you loaded... assuming just one thing there...
  theName <- ls(envir=e)[1]
  return(get(theName))
}
df <- sapply(temp, function(x)cbind(getList(x)))
链接地址: http://www.djcxy.com/p/38302.html

上一篇: 使用.RData文件加载rpart.object类型后,Rshiny发生错误

下一篇: 将Rdata的列表转换为R中的数据帧