Katherine
Katherine

Reputation: 1

Extracting and compiling specific rows of data from multiple csv files in R stuidio

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:

The numbers in the first column that correspond to 253, 254, and 251 in the third column is the data I'd like to extract

I'm a novice at best when it comes to coding. Thank you so much for any help!

Upvotes: 0

Views: 1025

Answers (1)

wmay
wmay

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

Related Questions