I am importing several .csv files of temperature registers from a folder, and need to create a single table mergind them all, in which the first 15 lines must be skipped and the file names must be kept. How to keep the file names?
Any advice is appreciated
Thanks
> setwd("")
>
> files <- list.files("folder",pattern = ".csv", recursive
> = T, full.names = T)
>
> data<- do.call(rbind, lapply
> (files, read.csv, as.is=T, skip = 15, header = TRUE))
With this code I get the table but do not know how to add a new variable with the file names
> data
time temp
1 2019-11-29 19:39:28 14.4
2 2019-11-29 20:09:28 14.4
3 2019-11-29 20:39:28 14.5
4 2019-11-29 21:09:28 14.5
You might want something along the lines
files = list.files("folder", pattern = "\\.csv$", recursive = TRUE, full.names = TRUE)
# data0 =
lapply(files, \(i) { # or basename(files)?
read.csv(i, as.is = TRUE, skip = 15L, header = TRUE) |>
transform(name = i) # or sub(".csv", "", i) instead of i
}) |> do.call(what = "rbind")
Edit:
Based on your comment I suggest
# data0 =
lapply(basename(files), \(i) { # or files
read.csv(i, skip = 15L) |>
transform(source = sub(".csv", "", i)) # basename here?
}) |> do.call(what = "rbind")