[Solved] Loop in R to get specific columns and the sum of the rest from different .csv files


First put all files in a single folder.

filenames <- list.files(pattern = ".csv")

all <- lapply(filenames, function(name) {
  readr:: read_csv(name)
})

Based on your description the list should have a length of 300, each item containing a dataframe. If you want to bind the rows of all dataframes you can use dplyr’s bind_rows(). Below is a solution based on an example dataset with 4 cols per dataset.

# assumed dataset post importing into a list from step 1
listOfDataFrames <- vector(mode = "list", length = 4)

for (i in 1:4) {
  listOfDataFrames[[i]] <- data.frame(Col1=sample(letters, 5, rep=F),
                                      Col2=rnorm(5), Col3=rnorm(5))
}


all_df <- dplyr::bind_rows(listOfDataFrames)
required_df <- all_df[,1:2]
sum_of_cols <- apply(all_df[,3:4], 2, sum)

===== edited answer based on new information =====

If all your csv files d001 to d300 are in the same folder you can apply the same principles that I have shared above (I’m not sure about the efficiency of the code, but it should still work).
Step 1: load all data frames into a single list
Step 2: loop through the list performing the same transformation on all dataframes (each element of list)
Step 3: loop through list writing each element of list into a separate csv file

Here is an example code:

filenames <- list.files(pattern = ".csv")

all <- lapply(filenames, function(name) {
  readr:: read_csv(name)
})


all_transformed <- lapply(all, function(df) {
  transmute(df, i, j, sum = sum(h01:h24, na.rm = TRUE))
})

names(all_transformed) <- paste0("d",1:length(all_transformed))
lapply(1:length(all_transformed), function(i) write.csv(all_transformed[[i]], 
                                                file = paste0(names(all_transformed[i]), ".csv"),
                                                row.names = FALSE))

3

solved Loop in R to get specific columns and the sum of the rest from different .csv files