abc_95
abc_95

Reputation: 179

Altering same column in multiple .csv files in R

I have a number of .csv files which are called "1_a.csv", "2_a.csv", "3_a.csv", "4_a.csv", and so on. Within each .csv file is a column called "participant", which currently has the participant number. For example, in "1_a.csv" under "participant" is 1, in "2_a.csv" under "participant" is 2.

What I would like to do is edit the column "participant" in each of these .csv files so that it also has the "_a", so in "1_a.csv" under "participant" is 1_a, in "2_a.csv" under "participant" is 2_a, and so on. However, I haven't been able to figure this out.

files <- list.files(path = 'data/', pattern = '*.csv', full.names = TRUE) # load data 

for (filename in files) {
  
  df = read.csv(filename)
  
  df$participant = df$participant + "_a"

}

Can anyone help?

Upvotes: 1

Views: 148

Answers (1)

akrun
akrun

Reputation: 887991

We can use lapply to loop over the files, read the data, do the transformation and store in a list. The + in R doesn't concatenate strings - use paste for that

lst1 <- lapply(files, function(x) {
       x1 <- transform(read.csv(x), 
     participant = paste0(participant, "_a"))
   write.csv(x1, file = x, quote = FALSE, row.names = FALSE)
   x1
})

In a for loop, the OP created a temporary object 'df', which gets updated in each iteration. Instead, we may need to store it in a list

lst1 <- vector('list', length(files))
names(lst1) <- files
for (filename in files) {
  
  df = read.csv(filename)
  
  df$participant = paste0(df$participant, "_a")
  lst1[[filename]] <- df 
  write.csv(df, file = filename, quote = FALSE, row.names = FALSE)

}

Upvotes: 2

Related Questions