Shalen
Shalen

Reputation: 95

add several csv files in the end of each other using R

Considering there are several files with different number of rows like the following:

  1. old people
  2. young people
  3. teenager people

enter image description here

Using R, I need to add them in one csv file at the end of each other with an extra column of the type of people (old, young , ...) like:

enter image description here

Cab you help me know which commands I should use for that.

Upvotes: 1

Views: 110

Answers (1)

akrun
akrun

Reputation: 887251

We can get load the '.csv' files in a list, read them with read_csv (from readr) by loopiing over the files with imap

library(purrr)
library(readr)
files <- list.files(pattern = '\\.csv', full.names = TRUE)
names(files) <-  tools::file_path_sans_ext(basename(files))
out <- imap_dfr(files, read_csv, .id = 'age')

Upvotes: 1

Related Questions