簡體   English   中英

如何使用嵌套的while循環保存for循環的輸出

[英]How to save the output of for loop with nested while loop

對於數據框中的每個緯度值,我正在計算一年中每一天(總共 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
  }
}

我正在尋找以下格式的輸出: 輸出

簡單的方法:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM