I have a folder with multiples files .txt ("qc11025010.txt" "qc11035010.txt" "qc11035020.txt" "qc11045010.txt"....) with meteorological data of various stations. Each of these files have 6 columns: Year, month, day, precipitation, maximum temperature and minimum temperature,and different lenght of rows
files[1]
V1 V2 V3 V4 V5 V6
1 1983 3 5 92.0 -99.9 -99.9
2 1983 3 6 141.0 -99.9 23.0
3 1983 3 7 61.3 -99.9 18.6
4 1983 3 8 10.7 -99.9 -99.9
5 1983 3 9 0.0 -99.9 -99.9
6 1983 3 10 0.0 -99.9 -99.9
files[2]
V1 V2 V3 V4 V5 V6
1 1983 3 15 0.6 -99.9 -99.9
2 1983 3 16 29.4 33.8 24.8
3 1983 3 17 23.2 28.0 -99.9
4 1983 3 18 0.6 -99.9 23.0
5 1983 3 19 0.5 33.8 23.4
6 1983 3 20 0.0 33.2 22.2
library(dplyr)
files <- list.files(path = folder, pattern = "txt")
dat <- read.table(files[1])
dat$ID <- rep(as.character(files[1]))
for (x in files[2:278]){
tb <- lapply(x, read.table, header=F)
tb$ID <- rep(as.character(x))
res <- rbind(datos, tb)
colnames(res) <- c("YEAR","MONTH","DAY","PCP","TMAX","TMIN", "ID")
}
Then, I obtain Error in rbind(deparse.level, ...) : invalid list argument: all variables should have the same length
I want to join the tables and add a unique ID of the following form
YEAR MONTH DAY PCP TMAX TMIN ID
1983 3 5 92.0 -99.9 -99.9 qc11025010.txt
1983 3 6 141.0 -99.9 23.0 qc11025010.txt
1983 3 7 61.3 -99.9 18.6 qc11025010.txt
.....
1983 3 15 0.6 -99.9 -99.9 qc11045010.txt
1983 3 16 29.4 33.8 24.8 qc11045010.txt
1983 3 17 23.2 28.0 -99.9 qc11045010.txt