Alex
Alex

Reputation: 3

Copy first line of multiple .csv files into new dataframe

I have over 1,000 .csv files in my directory. I need to select the first row (i.e. column names) of each of these files, and put them all into a new dataframe. Each .csv has a different number of columns.

Based on information from other stackoverflow questions, I've come up with this:

setwd("C:/Users/H300904/R project/files/CSV")

file_list <- list.files(getwd())
field_names <- data.frame(df)

for (file in file_list) {
   field_names <- read.csv(file="file", nrows=1, header=FALSE)
}

I need help referencing 'file' correctly and telling it to put the data on the next row in the new dataframe. Any tips?

Thank you for sharing your knowledge with me.

Upvotes: 0

Views: 525

Answers (1)

dario
dario

Reputation: 6483

file_list <- list.files(getwd())

field_names <- list()
for (i in seq_along(file_list) {
  field_names[i] <- read.csv(file=file_list[i], nrows=1, header=FALSE)
}

And then, if you really want to use a data.frame:

library(data.table)
rbindlist(field_names, fill=TRUE)

Edit:

As suggested by @r2evans, we probably would use lapply instead of the for loop (but they both do (almost) exactly the same).

field_names <- lapply(file_list, read.csv, nrows=1, header=FALSE)

Here, lapply iterates through the elements of file_list and passes them as the first argument to read.csv (together with the other arguments nrows=1, header=FALSE). Then lapply combines the results into a list.

Upvotes: 1

Related Questions