I'm an R beginner and trying to read in a number of csv files, remove/skip the last 5 rows from each one, and then rbind them together. I can't figure out which step to do the removal of the rows and what function to use? I've tried readLines
below and then tried to use nrow, but I'm pretty sure its in the wrong place.
This was what I started with:
alldata <- do.call(rbind, lapply(list.files(path = "./savedfiles", full.names = TRUE), read.csv))
I wasn't sure where to remove the rows in that code so I split it up to understand it and try to use readLines
:
files<- list.files(path = "./savedfiles", full.names = TRUE)
c <- lapply(files, readLines) - to count the rows
alldata<- do.call(rbind,lapply(files, nrow = length(f) - 5, full.names = TRUE), read.csv)
This is just throwing an error that argument FUN is missing, so I know I'm not doing it right but not sure how to fix it.
Something like this should put you on the right track. This reads the files first, then removes last 5 rows, and finally binds them together. Would also suggest not to use variable names that might conflict with function names. files
and c
are functions in base R. Here, I am using all_files
instead of files
. -
all_files <- list.files(path = "./savedfiles", full.names = TRUE)
do.call(rbind, # assuming columns match 1:1; use dplyr::bind_rows() if not 1:1
lapply(all_files, function(x) {
head(read.csv(x, header = T, stringsAsFactors = F), -5) # change as per needs
})
)