[R] Looping Through List of .csv Files to Work with Subsets of the Data
Chad Danyluck
c.danyluck at gmail.com
Mon Jun 8 23:48:37 CEST 2015
Hello,
I want to subset specific rows of data from 80 .csv files and write those
subsets into new .csv files. The data I want to subset starts on a
different row for each original .csv file. I've created variables that
identify which row the subset should start and end on, but I want to loop
through this process and I am not sure what to do. I've attempted to write
the loop below, albeit, much of it is pseudo code. If anyone can provide me
with some tips I'd appreciate it.
#### This data file is used to create the variables where the subsetting
starts and ends for each participant ####
mig.data <- read.csv("/Users/cdanyluck/Documents/Studies/MIG -
Dissertation/Data & Syntax/mig.data.csv")
# These are the variable names for the start and end of each subset of
relevant data (baseline, audio, and free)
participant.ids <- mig.processed.data$participant.id
participant.baseline.start <- mig.processed.data$baseline.row.start
participant.baseline.end <- mig.processed.data$baseline.row.end
participant.audio.start <- mig.processed.data$audio.meditation.row.start
participant.audio.end <- mig.processed.data$audio.meditation.row.end
participant.free.start <- mig.processed.data$free.meditation.row.start
participant.free.end <- mig.processed.data$free.meditation.row.end
# read into a list the individual files from which to subset the data
participant.files <- list.files("/Users/cdanyluck/Documents/Studies/MIG -
Dissertation/Data & Syntax/MIG_RAW DATA & TXT Files/Plain Text Files")
# loop through each participant
for (i in 1:length(participant.files)) {
# get baseline rows
results.baseline <-
participant.files[participant.baseline.start[i]:participant.baseline.end[i],]
# get audio rows
results.audio
<- participant.files[participant.audio.start[i]:participant.audio.end[i],]
# get free rows
results.free <-
participant.files[participant.free.start[i]:participant.free.end[i],]
# write out participant relevant data
write.csv(results.baseline, file="baseline[i].csv")
write.csv(results.audio, file = "audio[i].csv")
write.csv(results.free, file = "free[i].csv")
}
--
Chad M. Danyluck, MA
PhD Candidate, Psychology
University of Toronto
“There is nothing either good or bad but thinking makes it so.” - William
Shakespeare
[[alternative HTML version deleted]]
More information about the R-help
mailing list