Home > Enterprise >  R: assign ID to multiple data based on filename
R: assign ID to multiple data based on filename

Time:10-07

I want to read in multiple data files of seperate participants. The raw data doesn't contain participant ID, though, which is saved in the filename.

When I do the following, participant ID is assigned to rows in turns but not to the data it actually belongs to.

filenames_all <- dir(".", pattern = "*.log") 

for(file in 1:length(filenames_all)) {
  # load all files
  data <- read.csv(filenames_all, sep = "\t",  dec = ".", header = F)
  # assign ID based on filename
  data$ID <- substr(filenames_all, 1, 3)
} 

My data frame should ideally look like this:

filenames_all <- list("001-file.log", "002-file.log", "003-file.log")
data <- data.frame(V1 = rnorm(9,0,5), 
                   ID = c("001", "001","001","002","002","002","003","003","003"))

Yet it looks like this:

data <- data.frame(V1 = rnorm(9,0,5), 
                   ID = c("001", "002","003","001","002","003","001","002","003"))

Any help is very much appreciated.

CodePudding user response:

Try this:

data_all = lapply(filenames_all, function(fname) {
  data <- read.csv(fname, sep = "\t",  dec = ".", header = F)
  data$ID <- substr(fname, 1, 3)
  data
})
  • Related