Adding unique identifier variable to data frame from file name

Hi there!

I have multiple data frames that I read from a bunch of txt files in a folder using the easycsv package. Now I want to merge all the data frames together into one, but first I need to add a unique identifier variable to each data frame. Each data frame contains data from one participant. The number of rows for each data frame varies between participants. The participant number is 2 to 3 digits long and always directly follows the characters "VP" in the file name. How can I extract the participant number from the file name and add it as a new column to the data frames, preferrably in one go? I would really appreciate any help in this issue!

Thanks in advance!

You can use something like this, I cant test the solution since you are not providing any sample of the data or file names so you will have to adapt it to your actual scenario.

library(tidyverse)
library(stringr)

list_of_files <- list.files(path = "path/to/your/files",
                            recursive = TRUE,
                            pattern = ".txt$",
                            full.names = TRUE)
df <- list_of_files %>%
  setNames(nm = .) %>% 
  map_df(~read.csv(file = .x), .id = "participant") %>% 
  mutate(participant = str_extract(participan, "VP\\d{2,3}")
  )

Thanks for your help! I ended up solving this problem a different way and it overlapped with your reply. Sorry about that! I wish I had the energy to test your solution, but I'm having trouble doing other really basic stuff in R just to give you an idea of the level of beginner you're dealing with.

If your question's been answered (even if by you), would you mind choosing a solution? (See FAQ below for how).

Having questions checked as resolved makes it a bit easier to navigate the site visually and see which threads still need help.

Thanks

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.