I am trying to merge several csv files with the same columns in R. I tried the solution proposed in : Merging of multiple excel files in R and works perfectly. However, I would like to add an index column (row name) to identify which file each row corresponds to. I tried:
files <- list.files(pattern="*.csv")
require(purrr)
mainDF <- files %>% map_dfr(read.csv, row.names=files)
But I get the error:
Error in read.table(file = file, header = header, sep = sep, quote = quote, : invalid 'row.names' length
I would like to get a column similar to this, or ideally just the numbers e.g. 1, 2 etc
Any ideas?
row.names="files"
, you are giving the string "files", not the objectfiles
. Try removing the quotes. – coryrow.names
insideread.csv
, as this:mainDF <- files %>% map_dfr(read.csv(row.names=T))
? – AlvaroMartinezmainDF<- files %>% map_dfr(read.csv(row.names = 1, header= TRUE))
, in theoryrow.names=1
will set first.csv
column as row names. – AlvaroMartinez