Importing multiple .csv files into R based on a list

My data: I have longitudinal data for a few hundred subjects and the program spit out the datafiles in the format of week.subjectID.csv. For example, 1.100.csv is the file for week 1 of subject 100.

My problem: Using the post at the end, I figured out how to read in multiple files and combine them (code below).

library(plyr)
#Create list of all weekly files for subject ID 100
files = list.files(pattern="*.100.csv")
#List of data frames
subject = lapply(files, read.delim)
#Throw them altogether
exporty = ldply(subject, data.frame)
#Export new file
write.csv(exporty, "100")

However, this process is quite slow when having to repeat this command for all of my subjects. Is there a way to create a list of my subjects and, one subject at a time, read in their files, combine them, export their combined dataset, and then move onto the next subject?

Resource: Importing multiple .csv files into R

Answers


yes you can put your all code into another lapply():

library(plyr)

subjects <- c("100", "200", "300")
lapply(subjects, FUN=function(eachsubject){
    files = list.files(pattern=paste0("*.", eachsubject, ".csv"))
    subject = lapply(files, read.delim)
    exporty = ldply(subject, data.frame)
    write.csv(exporty, eachsubject)
    })

Need Your Help

Strange error with Prepared query is not of type SELECT_LONG

java android ormlite

I want to ask how can I make queries for this. I want to check the name from edit text and if the name exsist, as a part of record, in db i want to fill all edit texts with data from this object. A...

CORS Filter Error-- Need help to fix the same

javascript cors

I am trying to create a javascript+html which actually performs the action like CSRF (Cross site request forgery). So far, I am able to do this.