對于資料框中的每個緯度值,我正在計算一年中每一天(總共 365 天)的日長。因此,我使用 for-loop 回圈遍歷資料幀的行并使用 while-loop 來表示一年中的哪一天。但是,我不確定如何保存輸出。
for (i in 1:dim(df)[1]){
j<-1
while(j<=365){
output<-daylength(lat=df$latitude[i],doy=j)
j<-j 1
# save output
}
}
我正在尋找以下格式的輸出:

uj5u.com熱心網友回復:
簡單的方法:
output <- data.frame(latitude = numeric(0),
matrix(numeric(0), ncol=365))
names(output) <- gsub("X", "day", names(my_df))
for (i in seq_len(nrow(df))){
output[i, 1] <- df$latitude[i]
j <- 1
while(j <= 365){
output[i, j 1] <- daylength(lat = df$latitude[i], doy = j)
j <- j 1
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/343677.html
標籤:r
