Reputation: 1
I work in a labratory wherein we collected 100+ .csv files every day of recorded, time-locked events. There are specific timepoint-outputs that we need from each files and extracting these numbers by hand is not very efficient.
I'm wondering anyone has ideas of how to code a R script that would extract and compile these time points? I've been looking into the mcsv_r() function; however I need to know which file the time points came out of and I'm not sure if that function is capable.
Here's an image which may explain what I'm trying to accomplish better than I can, this is from a single file (file #1) of 30:
I'm a novice at best when it comes to coding. Thank you so much for any help!
Upvotes: 0
Views: 1025
Reputation: 234
Try something like this:
# get a list of the csv files in the directory
files = list.files(path = ".", pattern = "csv")
n = data.frame()
for (file in files) {
csv = read.csv(file)
# X3 is the default third column name -- you might have to change that
data = csv[csv$X3 %in% c(251, 253, 254), ]
data$file = file # add a new column with the filename
n = rbind(n, data)
}
write.csv(n, file = "compiled_data.csv")
Your image shows some blank fields in the first column. If you want to exclude those rows this will have to be slightly edited.
Upvotes: 1