Home > Software design >  Loop through list and generate and export objects for each element in the list
Loop through list and generate and export objects for each element in the list

Time:09-02

I am new to R coming from stata and struggling to adjust to the R language for loops. I need to loop over a list creating one table per each element in the list, and export each table to a .csv file. This is a shorter version of my data:

mydata <- structure(list(SampleID = c("R22 w0", "R24 w0", "R26 w0", "R29 w0", 
                               "R22 w1", "R24 w1", "R26 w1", "R29 w1", "R22 w8", "R24 w8", "R26 w8", 
                               "R29 w8", "R22 w24", "R24 w24", "R26 w24", "R29 w24", "R23 w0", 
                               "R25 w0", "R27 w0", "R30 w0", "R23 w1", "R25 w1", "R27 w1", "R30 w1", 
                               "R23 w8", "R25 w8", "R27 w8", "R30 w8", "R23 w24", "R25 w24", 
                               "R27 w24", "R30 w24", "R1 w0", "R3 w0", "R5 w0", "R7 w0", "R9 w0", 
                               "R11 w0", "R13 w0", "R15 w0", "R17 w0", "R19 w0", "R21 w0", "R1 w1", 
                               "R3 w1", "R5 w1", "R7 w1", "R9 w1", "R11 w1", "R13 w1", "R15 w1", 
                               "R17 w1", "R19 w1", "R21 w1", "R1 w8", "R3 w8", "R5 w8", "R7 w8", 
                               "R9 w8", "R11 w8", "R13 w8", "R15 w8", "R17 w8", "R19 w8", "R21 w8", 
                               "R1 w24", "R3 w24", "R5 w24", "R7 w12", "R9 w24", "R11 w24", 
                               "R13 w24", "R15 w24", "R17 w24", "R19 w48", "R21 w24", "R2 w0", 
                               "R4 w0", "R6 w0", "R8 w0", "R10 w0", "R12 w0", "R14 w0", "R16 w0", 
                               "R18 w0", "R20 w0", "R2 w1", "R4 w1", "R6 w1", "R8 w1", "R10 w1", 
                               "R12 w1", "R14 w1", "R16 w1", "R18 w1", "R20 w1", "R2 w8", "R4 w8", 
                               "R6 w8", "R8 w4", "R10 w8", "R12 w8", "R14 w8", "R16 w8", "R18 w8", 
                               "R20 w8", "R2 w24", "R4 w24", "R6 w24", "R8 w24", "R10 w24", 
                               "R12 w24", "R14 w24", "R16 w24", "R18 w24", "R20 w48"), week = c(0, 
                                                                                                0, 0, 0, 1, 1, 1, 1, 8, 8, 8, 8, 24, 24, 24, 24, 0, 0, 0, 0, 
                                                                                                1, 1, 1, 1, 8, 8, 8, 8, 24, 24, 24, 24, 0, 0, 0, 0, 0, 0, 0, 
                                                                                                0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 8, 8, 8, 8, 8, 8, 
                                                                                                8, 8, 8, 8, 8, 24, 24, 24, 12, 24, 24, 24, 24, 24, 24, 24, 0, 
                                                                                                0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 8, 8, 
                                                                                                8, 4, 8, 8, 8, 8, 8, 8, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24
                               ), Treatment = c("Placebo", "Active", "Placebo", "Active", "Placebo", 
                                                "Active", "Placebo", "Active", "Placebo", "Active", "Placebo", 
                                                "Active", "Placebo", "Active", "Placebo", "Active", "Active", 
                                                "Placebo", "Placebo", "Active", "Active", "Placebo", "Placebo", 
                                                "Active", "Active", "Placebo", "Placebo", "Active", "Active", 
                                                "Placebo", "Placebo", "Active", "Active", "Placebo", "Placebo", 
                                                "Active", "Placebo", "Active", "Placebo", "Placebo", "Active", 
                                                "Placebo", "Placebo", "Active", "Placebo", "Placebo", "Active", 
                                                "Placebo", "Active", "Placebo", "Placebo", "Active", "Placebo", 
                                                "Placebo", "Active", "Placebo", "Placebo", "Active", "Placebo", 
                                                "Active", "Placebo", "Placebo", "Active", "Placebo", "Placebo", 
                                                "Active", "Placebo", "Placebo", "Active", "Placebo", "Active", 
                                                "Placebo", "Placebo", "Active", "Placebo", "Placebo", "Active", 
                                                "Active", "Placebo", "Placebo", "Placebo", "Active", "Active", 
                                                "Placebo", "Active", "Active", "Active", "Active", "Placebo", 
                                                "Placebo", "Placebo", "Active", "Active", "Placebo", "Active", 
                                                "Active", "Active", "Active", "Placebo", "Placebo", "Placebo", 
                                                "Active", "Active", "Placebo", "Active", "Active", "Active", 
                                                "Active", "Placebo", "Placebo", "Placebo", "Active", "Active", 
                                                "Placebo", "Active", "Active")), row.names = c(NA, -116L), class = "data.frame")

I need a loop to apply the following code for each possible value in the column "week" (0, 1, 4, 8, 12, 24 and 48).

Data_week_0 <-  mydata %>% filter(!stringr::str_detect(Treatment, 'Placebo'))

So that I get 5 Data_week_ tables with the 0, 1, 4, 8, 12, 24 and 48 suffix. And then, export each object to a .csv file

write.csv(Data_week_0,"~/Data_week_0.csv", row.names = FALSE)

I know this is a basic question, but I will appreciate any help.

CodePudding user response:

Here is an option to loop through your data to export individual week dataframe for each treatment condition:

library(tidyverse)

for (j in unique(mydata$Treatment)) {
  df_treatment = mydata %>% filter(Treatment==j)
  for (i in unique(df_treatment$week)) {
    df_week <-  df_treatment %>% filter(week==i)
    write.csv(df_week, paste0("~/Data_week_", i, "_", j,".csv"), row.names = FALSE)
  }
}

CodePudding user response:

No need to use tidyverse here. You can use base R:

# week values to filter by, assign object
weekVals <- c(0, 1, 4, 8, 12, 24, 48)

# treatment values to filter by, assign object
treatVals <- unique(mydata[["Treatment"]])

# combine values and iterate over each row..
combVals <- expand.grid(weekVals, treatVals, stringsAsFactors = FALSE)

# use for loop to iterate over rows...
for (i in seq_len(nrow(combVals))) {
  
  wk <- combVals[i, 1]
  treat <- combVals[i, 2]
  
  # subset data
  df <- mydata[mydata[["week"]] == wk & mydata[["Treatment"]] == treat, ]
  
  # tell user which subsets return no match
  if (nrow(df) == 0L) {cat("Data frame is empty for combination week", wk, ", Treatment:", treat, "\n\n");next}
  
  # write it out
  write.csv(df, paste0("Data_week_", wk, "_", treat, ".csv"))
}

Can also use data.table to filter and save it to csv with function fwrite:

# data.table and for loop, fwrite to write file into csv
for (i in seq_len(nrow(combVals))) {
  
  wk <- combVals[i, 1]
  treat <- combVals[i, 2]
  
  # subset data
  df <- mydata[week == wk & Treatment == treat, ]
  
  # tell user which subsets return no match
  if (nrow(df) == 0L) {cat("Data frame is empty for combination week", wk, ", Treatment:", treat, "\n\n");next}
  
  # write it out
  data.table::fwrite(df, paste0("Data_week_", wk, "_", treat, ".csv"))
}
  • Related