我无法循环遍历数据框列表。我将附上我的完整代码与我的文件路径编辑,然后解释具体细节。
# load packages ----
xlib <- c("rnoaa","devtools","dplyr","plyr","hydroTSM","chron","gdata","date", "rowr")
lapply(xlib, require, character.only = T)
rm(xlib)
# se token ----
# get this from NOAA site - https://www.ncdc.noaa.gov/cdo-web/token
options(noaakey = "QlzFUrMWVrLFKkZijFohYRmbtVvEaqUB")
# set working folder ----
setwd("filepathway")
# read weather station information
statns <- read.csv(file = "filepathway/weather_station_locations_v1.csv", header = T)
# define dates ----
st.date <- as.Date("1950-01-01")
end.date <- as.Date("2018-04-30")
date1 <- data.frame(date = dip(st.date, end.date))
date1$year <- as.numeric(format(date1$date, "%Y"))
date1$month <- as.numeric(format(date1$date, "%m"))
date1$day <- as.numeric(format(date1$date, "%d"))
date1$jday <- as.numeric(format(date1$date, "%j"))
n1 <- 1
#n1 <- 100
for(id1 in (1:n1)) {
# pull data from NOAA server ----
# 1. precip
prcp.pull <- meteo_pull_monitors(monitors = statns$STATION[ id1 ],
keep_flags = T,
date_min = st.date,
date_max = end.date,
var = "PRCP")
prcp.pull$prcp <- prcp.pull$prcp / 10 # convert to mm/day
# 2. max. temperature
tmax.pull <- meteo_pull_monitors(monitors = statns$STATION[ id1 ],
keep_flags = T,
date_min = st.date,
date_max = end.date,
var = "TMAX")
tmax.pull$tmax <- tmax.pull$tmax / 10 # convert to dec. C
#3. min. temperature
tmin.pull <- meteo_pull_monitors(monitors = statns$STATION[ id1 ],
keep_flags = T,
date_min = st.date,
date_max = end.date,
var = "TMIN")
tmin.pull$tmin <- tmin.pull$tmin / 10 # convert to dec. C
statns2 <- split(statns, statns$"STATION")
colnames(prcp.pull)[1] <- "STATION"
colnames(tmin.pull)[1] <- "STATION"
colnames(tmax.pull)[1] <- "STATION"
prcpA <- rbind.fill(statns2, prcp.pull)
tminA <- rbind.fill(statns2, tmin.pull)
tmaxA <- rbind.fill(statns2, tmax.pull)
prcpB <- cbind.fill(statns2, prcpA)
tminB <- cbind.fill(statns2, tminA)
tmaxB <- cbind.fill(statns2, tmaxA)
tminC <- merge(tminB, statns2, by.x = 2, by.y = 2)
tmaxC <- merge(tmaxB, statns2, by.x = 2, by.y = 2)
prcpC <- merge(prcpB, statns2, by.x = 2, by.y = 2)
colnames(tminC)[2] <- "A"
colnames(tminC)[3] <- "B"
colnames(tminC)[4] <- "C"
colnames(tminC)[5] <- "D"
colnames(tminC)[6] <- "E"
colnames(tminC)[7] <- "G"
tminD = subset(tminC, select = -c(A, B, C, D, E, G ))
colnames(tmaxC)[2] <- "A"
colnames(tmaxC)[3] <- "B"
colnames(tmaxC)[4] <- "C"
colnames(tmaxC)[5] <- "D"
colnames(tmaxC)[6] <- "E"
colnames(tmaxC)[7] <- "G"
tmaxD = subset(tmaxC, select = -c(A, B, C, D, E, G ))
colnames(prcpC)[2] <- "A"
colnames(prcpC)[3] <- "B"
colnames(prcpC)[4] <- "C"
colnames(prcpC)[5] <- "D"
colnames(prcpC)[6] <- "E"
colnames(prcpC)[7] <- "G"
prcpD = subset(prcpC, select = -c(A, B, C, D, E, G ))
# save output as text file
fout <- paste("filepathway", c("prcpD", "tmaxD", "tminD"), statns$STATION[ id1 ],
".csv", sep = "")
# 1. precip
if (nrow(prcpD) > 0) {
write.table(file = fout[1], x = prcpD, col.names = T, row.names = T, append = F, sep = ",", quote = F)
}
# 2. tmax
if (nrow(tmaxD) > 0) {
write.table(file = fout[2], x = tmaxD, col.names = T, row.names = T, append = F, sep = ",", quote = F)
}
# 3. tmin
if (nrow(tminD) > 0) {
write.table(file = fout[3], x = tminD, col.names = T, row.names = T, append = F, sep = ",", quote = F)
}
}
}
在这一行之后:statns2&lt; - split(statns,statns $“STATION”)我得到一个数据帧列表,并希望在每个这些单独的数据帧中运行循环 - 例如,当id1(a数字从1到13926)匹配FID + 1(FID从0开始并到达列表的末尾),这样分割后的逗号一次一个地运行到列表中,确保匹配我的降水之间的信息,温度数据和气象站
如果没有拆分成数据框列表,它只会为每个气象站提供一行数据,而我想要一行识别该站,然后从开始到结束每个日期一行。
更新:我创建了我的列表的一小部分,并使用了head(statns.small)并粘贴了我的结果
$`CA003030525`
FID STATION LAT LON ELEV NAME CODE
1 0 CA003030525 49.8 -112.3 824 AB BARNWELL AGDM 71346
$CA003030720
FID STATION LAT LON ELEV NAME CODE
2 1 CA003030720 49.5667 -113.05 980 AB BLOOD TRIBE AGDM 71517
$CA003030768
FID STATION LAT LON ELEV NAME CODE
3 2 CA003030768 49.7333 -111.45 817 AB BOW ISLAND 71231