Richard Michaud Langis
Richard Michaud Langis

Reputation: 133

How to do a loop of operations that can adjust to the amount of files I have

so I have 4 groups of data(PRE,DBG,DBN,POST) with up to 4 parts each (per participants). I have a code here:

rm(list = ls())
PRE1 <- read.csv("AE1_01 PRE Part 1 Angles_cleaned_rula.csv")
PRE2 <- read.csv("AE1_01 PRE Part 2 Angles_cleaned_rula.csv")
PRE3 <- read.csv("AE1_01 PRE Part 3 Angles_cleaned_rula.csv")
PRE4 <- read.csv("AE1_01 PRE Part 4 Angles_cleaned_rula.csv")

DBG1 <- read.csv("AE1_01 DBG Part 1 Angles_cleaned_rula.csv")
DBG2 <- read.csv("AE1_01 DBG Part 2 Angles_cleaned_rula.csv")
DBG3 <- read.csv("AE1_01 DBG Part 3 Angles_cleaned_rula.csv")
DBG4 <- read.csv("AE1_01 DBG Part 4 Angles_cleaned_rula.csv")

DBN1 <- read.csv("AE1_01 DBN Part 1 Angles_cleaned_rula.csv")
DBN2 <- read.csv("AE1_01 DBN Part 2 Angles_cleaned_rula.csv")
DBN3 <- read.csv("AE1_01 DBN Part 3 Angles_cleaned_rula.csv")
DBN4 <- read.csv("AE1_01 DBN Part 4 Angles_cleaned_rula.csv")

POST1 <- read.csv("AE1_01 POST Part 1 Angles_cleaned_rula.csv")
POST2 <- read.csv("AE1_01 POST Part 2 Angles_cleaned_rula.csv")
POST3 <- read.csv("AE1_01 POST Part 3 Angles_cleaned_rula.csv")
POST4 <- read.csv("AE1_01 POST Part 4 Angles_cleaned_rula.csv")

(I know this is probably not the best way to do it, but oh well)

So after commenting out the parts of each group that I do not have, ei PRE part 1,2,3 DBG part 1, DBN part 1,2 , POST part 1,2,3,4

How can I have a loop that would:

  1. remove columns like PRE1 <- subset (PRE1, select = -c(1:25,27,28))
  2. bind all parts per group together with rbind() into one df per group so that no matter what, at the end of this I have 1 df per group (4 in total).

UPDATE* I am fine adapting the number of import files by commenting them out if I don't have them. I just need a loop that (maybe I can adjust a number like for(i=3) if I have 3 parts) will run through the 2 things I need mentioned above

Upvotes: 0

Views: 37

Answers (1)

br00t
br00t

Reputation: 1614

This solution should work for an arbitrary number of files and/or groups:

rm(list = ls())
PRE1 <- read.csv("AE1_01 PRE Part 1 Angles_cleaned_rula.csv")
PRE2 <- read.csv("AE1_01 PRE Part 2 Angles_cleaned_rula.csv")
PRE3 <- read.csv("AE1_01 PRE Part 3 Angles_cleaned_rula.csv")
PRE4 <- read.csv("AE1_01 PRE Part 4 Angles_cleaned_rula.csv")

DBG1 <- read.csv("AE1_01 DBG Part 1 Angles_cleaned_rula.csv")
DBG2 <- read.csv("AE1_01 DBG Part 2 Angles_cleaned_rula.csv")
DBG3 <- read.csv("AE1_01 DBG Part 3 Angles_cleaned_rula.csv")
DBG4 <- read.csv("AE1_01 DBG Part 4 Angles_cleaned_rula.csv")

DBN1 <- read.csv("AE1_01 DBN Part 1 Angles_cleaned_rula.csv")
DBN2 <- read.csv("AE1_01 DBN Part 2 Angles_cleaned_rula.csv")
DBN3 <- read.csv("AE1_01 DBN Part 3 Angles_cleaned_rula.csv")
DBN4 <- read.csv("AE1_01 DBN Part 4 Angles_cleaned_rula.csv")

POST1 <- read.csv("AE1_01 POST Part 1 Angles_cleaned_rula.csv")
POST2 <- read.csv("AE1_01 POST Part 2 Angles_cleaned_rula.csv")
POST3 <- read.csv("AE1_01 POST Part 3 Angles_cleaned_rula.csv")
POST4 <- read.csv("AE1_01 POST Part 4 Angles_cleaned_rula.csv") 

# create a list which defines which group each data.frame belongs to
groups <- lapply(c('PRE', 'DBG', 'DBN', 'POST'), 
       function(x) ls(pattern = sprintf('%s[0-9]+', x), envir = .GlobalEnv))
# iterate over groups
results <- lapply(groups, function(x) {
  # extract subset of columns for each df
  df_list <- lapply(x, function(y) {
    dfy <- get(y, envir = .GlobalEnv)
    dfy <- subset(dfy, select = -c(1:25,27,28))
    dfy
  })
  # combine all data.frames for this group into a single one
  df <- data.table::rbindlist(df_list) |> 
    as.data.frame()                  
  # return combined data.frame to caller
  df
})

View(results)

Upvotes: 2

Related Questions