0

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.

AnilGoyal
  • 25,297
  • 4
  • 27
  • 45
jp0121
  • 1
  • 2
  • I suggest you read https://stackoverflow.com/a/24376207/3358227, its tricks for using `lapply` is what you need here. – r2evans Jun 30 '21 at 00:23

2 Answers2

1

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

sammy
  • 427
  • 1
  • 3
  • 14
  • Yes, the third column has the same title in all files. I also should have clarified, and I have updated the original post, but this is for every file in a folder. So I have 172 files in one folder. – jp0121 Jun 30 '21 at 00:32
0

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
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213