Dealing with data from the same user twice when moving through a loop in R

Viewed 27

Let's say I have a dataframe that looks a bit like this:

df <- tribble(
  ~person_id, ~timestamp,
  "1", "02:26:10.000000",
  "1", "03:45:37.000000",
  "2", "22:03:39.000000",
  "3", "11:46:24.000000",
  "4", "18:26:55.000000",
  "5", "17:01:20.000000",
  "5", "03:10:17.000000",
  "6", "23:16:05.000000",
)
df

Now let's say I import individual .csv files that each match the person_id like so:

  user_files <- list.files(pattern = "\\.csv$", path = here("data"),
                           full.names = TRUE)
  
  user_files <- user_files[sub("\\.csv$", "", basename(user_files)) %in% df$person_id]

There will naturally be fewer .csv files than the length of df$person_id because persons "1" and "5 appear twice in df$person_id

I would now like to run a for loop that runs a program on each csv file. HOWEVER, where there are more than one of the same person_id, I would like to re-run the loop using the same csv file (since it's the same person again but a different timestamp so will yield different results).

This is what the loops look like

 for(i in seq(1:length(user_files))) {

    user_file <- read_csv(user_files[i])

#Run lots of analysis on the CSV file

}

Now I need something in the loop that says "if df$person_id occurs more than once, repeat the loop using the same CSV file". Thanks in advance for any assistance.

1 Answers

If the user_files are unique, then match the user_files with the 'df$person_id' use that index to subset the 'user_files'

v1 <- sub("\\.csv$", "", basename(user_files))
user_files2 <- na.omit(user_files[match(df$person_id, v1)])

Now, loop over 'user_files2'


Or a better approach is to merge/join with the original dataset and loop over the filtered data user_files column

library(dplyr)
df1 <- inner_join(df, tibble(user_files,
    person_id = sub("\\.csv$", "", basename(user_files))))
Related