[英]How to save the output of for loop with nested while loop
For every value of latitude in a dataframe, I am calculating the daylength for each day (total 365 days) of the year.对于数据框中的每个纬度值,我正在计算一年中每一天(总共 365 天)的日长。 So, I have used for-loop to loop over the rows of dataframe and used while-loop to represent the day of year.因此,我使用 for-loop 循环遍历数据帧的行并使用 while-loop 来表示一年中的哪一天。 But, I am not sure how to save the output.但是,我不确定如何保存输出。
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
}
}
I am looking for the output to be in the following format:我正在寻找以下格式的输出:
The simple method:简单的方法:
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
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.