0
votes

I'm just getting used to using lapply and I've been trying to figure out how I can use names from a vector to append within filenames I am calling, and naming new dataframes. I understand I can use paste to call the files of interest, but not sure I can create the new dataframes with the _var name appended.

site_list <- c("site_a","site_b", "site_c","site_d")
lapply(site_list,
  function(var) {
    all_var <- read.csv(paste("I:/Results/",var,"_all.csv"))
    tbl_var <- read.csv(paste("I:/Results/",var,"_tbl.csv"))
    rsid_var <-  read.csv(paste("I:/Results/",var,"_rsid.csv"))
    return(var)
})
1
You are returning 'var'. what is. your expected output. Do you want to get all the datasets as a list - akrun
You aren't doing anything with the tables you are reading in. - Axeman
Perhaps also read this. - Axeman
Wasn't sure if that should be in there to terminate the function. Trying to bring all the files (over 40 names in the vector with associated subfiles) into R at once for manipulation. - mgtrek
@Axeman Thanks! I'll take a look. - mgtrek

1 Answers

0
votes

Generally, it often makes more sense to apply a function to the list elements and then to return a list when using lapply, where your variables are stored and can be named. Example (edit: use split to process files together):

files <- list.files(path= "I:/Results/", pattern = "site_[abcd]_.*csv", full.names = TRUE)
files <- split(files, gsub(".*site_([abcd]).*", "\\1", files))
processFiles <- function(x){
    all <- read.csv(x[grep("_all.csv", x)])
    rsid <- read.csv(x[grep("_rsid.csv", x)])
    tbl <- read.csv(x[grep("_tbl.csv", x)])
    # do more stuff, generate df, return(df)
}
res <- lapply(files, processFiles)