Home > Mobile >  When binding together a list of csv tables with purrr as dataframe i want to include a tag column co
When binding together a list of csv tables with purrr as dataframe i want to include a tag column co

Time:03-07

I am binding togeter participant raw data from 33 csv files with the following code

filenames <- list.files(pattern = '*.csv', recursive = TRUE)

result <- purrr::map_df(filenames, read.csv, .id = 'id')

This works great. Now I need to to include a tag per participant(csv) in the final dataframe to make clear in which of several randomized conditions they were in.
I want to make it conditional on the first word in my first column of each .csv, as each participant got one of several randomized sequences of words.

I thought of something with ifelse() but not sure how to include this is in above code. I am a total R noob, any help is appreciated!

CodePudding user response:

I think this should achieve what you're looking for:

result <- lapply(result, function(x) { x$tag <- x[1, 1]; x })

do.call(rbind, result)
  • Related