Load all R data files from specific folder -
i've got lot of rdata files want combine in 1 dataframe.
my files, example, are:
file1.rdata file2.rdata file3.rdata
all datafiles have structure: datafile$a , datafile$b. of files above load take variable $a
and add , existing dataframe called md
. problem isn't loading files global environment, processing data in rdata file.
my code far, doesn't work.
library(dplyr) files <- list.files("correct directory", pattern="*.rdata")
this returns correct list of files.
i know need lapply
on function.
lapply(files, myfun)
my problem in function. i've got @ moment:
myfun <- function(files) { load(files) df <- data.frame(datafile$a) md <- bind_rows(md, df) }
the code above doesn't work, idea how work?
try
library(dplyr) bind_rows(lapply(files, myfun)) # #1 1 #2 2 #3 3 #4 4 #5 5 #6 1 #7 2 #8 3 #9 4 #10 5 #11 6 #12 7 #13 8 #14 9 #15 10 #16 11 #17 12 #18 13 #19 14 #20 15
where
myfun <- function(files) { load(files) df <- data.frame(a= datafile$a) }
data
datafile <- data.frame(a=1:5, b=6:10) save(datafile, file='file1.rdata') datafile <- data.frame(a=1:15, b=16:30) save(datafile, file='file2.rdata') files <- list.files(pattern='file\\d+.rdata') files
Comments
Post a Comment