jp0121
jp0121

Reputation: 11

How can I take one column from multiple CSV files to create a new dataframe in R?

Essentially I am looking to take the third column from every CSV file in a folder and append it to a data frame as a new column. Ideally I would like the header for each column to be the respective file name. I have about 172 files in the folder, each with a unique filename (i.e. file1.csv, file2.csv, etc) however the title of the third column is the same. Illustrating this on a smaller scale, if I had file 1 and file 2, the output would look like what is shown below.

File1

File2

Output

EDIT: added some clarification.

Upvotes: 1

Views: 1165

Answers (2)

Ronak Shah
Ronak Shah

Reputation: 388817

You can use lapply to read all the files, extract the 3rd column from it, assign the name of the column same as the file name and bind all such column together in one dataframe.

filenames <- list.files(pattern = '.csv$', full.names = TRUE)

do.call(cbind, lapply(filenames, function(x) {
  setNames(data.frame(read.csv(x)[[3]]), tools::file_path_sans_ext(basename(x)))
})) -> result

result

Upvotes: 0

sammy
sammy

Reputation: 437

Will your third column always be the same name in both files?

if not you could to the below

cbind(file1[,3], file2[,3])

cbind would combine the data frames by column

Upvotes: 1

Related Questions