Kris
Kris

Reputation: 63

I'd like to delete the top (header) row from a list of multiple tables

I read in multiple .csv files from a directory using list.files(path, pattern=".csv"), then lapply(data, read.csv) which opens them as a list of tables in R. The headers are attached (like they're part of the data) & I'd like to delete the first row from each table in the list to eliminate them & make my own headers. I was able to do this when I read in 1 file at a time using lapply(data[-1,]) but now it's not working on the list of tables.. Do I have to turn them into a data frame first? If so I'm not sure how to go about this in a data frame..? Thx in advance

Upvotes: 0

Views: 908

Answers (1)

Soheil
Soheil

Reputation: 974

Suppose you want to change the header of the files in your directory, then attach them together:

myfun <- function(x) {
  dataset <- fread(x,header=TRUE, sep=",")
  setnames(dataset,c("Name1","Name2"))
  return(dataset)
}

data <- rbindlist(lapply(list.files(),myfun))

Upvotes: 0

Related Questions