简体   繁体   中英

R - For loop over large list of elements

I have split my large data set by date like so to create a large list of several elements:

days <- split(df, df$Date)

My data has columns including time of sunrise, sunset etc. for each day. I now want to use a for loop to do further work on each day separately like this:

for(i in 1:length(days){
sunrisetime <- as.character(df$Sunrise[1])
# Further similar work (using time of sunrise & sunset for each date to split 
into daytime hours and nighttime hours)
}

My question is about the df$Sunrise on the second line - I don't think this is the right code to use when trying to access the sunrise time of each day on the days list. I have tried all sorts of variations but am an R newbie so must just be hitting the wrong terms.

Thanks in advance.

sunrisetime<-rep(NA,length(days))

for(i in 1:length(days){
  sunrisetime[i] <- as.character(df$Sunrise[i]) 
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM