For R, how do I row bind an unknown number of items in list?

I am new to R. I have many csv files that I want to append into a single CSV file.

I have the following issues in my below code:

  1. After the 3rd line Error in read.table(file = file, header = header, sep = sep, quote = quote, : argument "file" is missing, with no default **
  2. I don't know the number of files in the folder to rowbind. So how do I amend this line of code to rowbind an unknown number of files? bind_rows(tables [1], tables [2], tables[3])

Thank you!

library(dplyr)
files <- list.files(path = "C:/Users/...",pattern="*.csv", full.names = T)
tables <- lapply(files, read.csv(header=False))
merged <- bind_rows(tables [1], tables [2], tables[3])
write.csv(merged, file = "C:/.../files_merged.csv", row.names = FALSE)

I would recommend using something like this instead

library(tidyverse)

list_of_files <- list.files(path = "C:/Users/...",
                            pattern = "\\.csv$",
                            full.names = TRUE)
df <- list_of_files %>% 
  map_dfr(read.csv, header = FALSE)

write.csv(df, file = "C:/.../files_merged.csv", row.names = FALSE)
1 Like

Hi andresrcs,

Thanks for the reply. But I get the error whether I use rbind or map_dfr. The values below the column head gets affixed with 

My csv files contain different number of rows & columns, and some rows are just text.

Warning messages:

1: In bind_rows_(x, .id) : Unequal factor levels: coercing to character

2: In bind_rows_(x, .id) :

binding character and factor vector, coercing into character vector

3: In bind_rows_(x, .id) :

binding character and factor vector, coercing into character vector

I read that rbindlist can solve the above issue of factors with unequal levels and character cols automatically. But I can't install the package data.table

how about, instead of read.csv use readr::read_csv , this wont guess factors, so you will get character columns that can be rowbound, then as a final step you can convert final columns of your choice to factor where those are your preferred types.

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.