0

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

Community
  • 1
  • 1
erebusgw
  • 67
  • 4
  • Note that the `pattern` argument to `ls` is _not_ a wildcard expression, but a regexp. What you want is `ls(pattern="\\.100\\.csv$"`. – Hong Ooi Mar 09 '16 at 00:00

1 Answers1

2

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)
    })
HubertL
  • 19,246
  • 3
  • 32
  • 51
  • Thank you so much! I thought it would be lapply, but couldn't quite put it together...especially with the naming of files. – erebusgw Mar 09 '16 at 15:04