循环在第一次迭代后停止吗?

时间:2019-07-02 03:04:07

标签: r loops

当前,我正在尝试对总线列表中的所有总线进行for循环,但是在第一次迭代之后,代码停止了。如果有人能给我反馈为什么会发生这种情况,我将不胜感激

#Read list of bus lines & stations(of ALL bus lines)
buslist<-read.csv("Buslist.csv")
stationlistAll<-read.csv("Stationlist.csv")

for(i in nrow(buslist)){
  #================================================= Occupancy calculation part ============================
  line_ID=buslist[i,1]
  line_NO=as.character(buslist[i,4])

  # Extract bus data of one line
  oneline <- subset(total_wbus,total_wbus$LineID == as.character(line_ID)) # Get bus number wanted
  #oneline$Departure <- as.POSIXct(as.character(oneline$Departure),"%Y%m%d%H%M%S", tz="Asia/Seoul") # convert time to POSIXct format
  #oneline$Boardtime <- as.POSIXct(as.character(oneline$Boardtime),"%Y%m%d%H%M%S", tz="Asia/Seoul") # convert time to POSIXct format
  #oneline$Alighttime <- as.POSIXct(as.character(oneline$Alighttime),"%Y%m%d%H%M%S", tz="Asia/Seoul") # convert time to POSIXct format
  oneline <- transform(oneline, dispatch=as.numeric(factor(Departure))) # grouping by dispatch order
  oneline$Alight_tag <- oneline$Alightstation!="~" # TRUE if the passenger tagged his/her card when alight

  # Add station order
  stationlist <- subset(stationlistAll, LineID==as.character(line_ID))[,2:3]
  oneline$Boardstation <- as.integer(oneline$Boardstation)
  oneline <- merge(oneline, stationlist, by.x="Boardstation", by.y="StationID", all.x=T)
  colnames(oneline)[which(names(oneline) == "StationNumber")] <- "Boardorder"
  oneline$Alightstation <- as.integer(oneline$Alightstation)
  oneline <- merge(oneline, stationlist, by.x="Alightstation", by.y="StationID", all.x=T)
  colnames(oneline)[which(names(oneline) == "StationNumber")] <- "Alightorder"

  # Separate boarding and alighting data
  record_board <- oneline[,c("Departure","dispatch","Boardstation","Boardorder", "Boardtime","Multiple","Alight_tag")]
  names(record_board) <- c("Departure","dispatch","StnID","stnorder","Time","Board","Alight_tag")
  record_alight <- oneline[,c("Departure","dispatch","Alightstation","Alightorder","Alighttime","Multiple","Alight_tag")]
  names(record_alight) <- c("Departure","dispatch","StnID","stnorder","Time","Alight","Alight_tag")
  record <- rbind(record_board, record_alight, fill=T)
  record$Board[is.na(record$Board)] <- 0
  record$Alight[is.na(record$Alight)] <- 0

  # calculate occupancy
  record$diff_tot <- record$Board - record$Alight
  #record$diff_tag[record$Alight_tag==T] <- 
record$diff_tot[record$Alight_tag==T] # include non-alight-tag
  record$diff_tag[record$Alight_tag==F] <- 0 # exclude non-alight-tag
  record <- record[order(record$dispatch, record$Time),] # sort by dispatch and time
  record$occ_tot <- ave(record$diff_tot, record$dispatch, FUN=cumsum) # include non-alight-tag
  record$occ_tag <- ave(record$diff_tag, record$dispatch, FUN=cumsum) # exclude non-alight-tag
  record<-record[,c('Departure','dispatch','StnID','stnorder','Time','Board','Alight','diff_tot','Alight_tag','occ_tot','occ_tag')]

  # Complete data making
  oneline_total <- subset(total_wbus,total_wbus$LineID == as.character(line_ID)) #Get bus number wanted
  oneline_total <- transform(oneline_total, dispatch=as.numeric(factor(Departure))) #grouping by dispatch order
  oneline_total <- oneline_total[with(oneline_total, order(Departure, Boardtime)), ] #order by Departure and boarding time

  stationlist <- subset(stationlistAll, LineID==as.character(line_ID))

  if(nrow(oneline_total)!=0){
    numdispatch <- max(oneline_total$dispatch)
    disp_stn_combi <- expand.grid(dispatch=1:numdispatch, StnID=factor(stationlist$StationID))
    disp_stn_combi <- merge(disp_stn_combi, stationlist[c("StationID","StationNumber")], by.x=c("StnID"), by.y=c("StationID"), all.x=TRUE) #add station sequence
    disp_stn_combi <- disp_stn_combi[order(disp_stn_combi$dispatch, disp_stn_combi$StationNumber),]
    disp_stn_combi <- disp_stn_combi[c("dispatch","StnID","StationNumber")]
    colnames(disp_stn_combi) <- c("dispatch","StnID","stnseq")

    oneline_total <- transform(oneline_total, Boardstation = 
as.numeric(as.character(Boardstation)), Alightstation = as.numeric(as.character(Alightstation)))

    count_tot_board <- as.data.frame(xtabs(Board ~ dispatch + stnorder, data = record)) # count boarded people
     #count_tot_board <- as.data.frame(count(record, c("dispatch", "stnorder", "Board"))) # count boarded people
    count_tot_alight <- as.data.frame(xtabs(Alight ~ dispatch + stnorder, data = record)) # count alighted people
    #count_tot_alight <- as.data.frame(count(record, c("dispatch", "stnorder", "Alight"))) # count boarded people
    count_tot <- merge(disp_stn_combi, count_tot_board, by.x=c("dispatch", "stnseq"), by.y=c("dispatch","stnorder"), all.x=TRUE)
    count_tot <- merge(count_tot, count_tot_alight, by.x=c("dispatch", "stnseq"), by.y=c("dispatch","stnorder"), all.x=TRUE)
    colnames(count_tot) <- c("dispatch","stnseq","stnID","boardpeople","alightpeople") # change column name
    count_tot[is.na(count_tot)] = 0 # NA to 0
    count_tot <- arrange(count_tot, dispatch, stnseq)
    count_tot$difference <- count_tot$boardpeople-count_tot$alightpeople 
# calculate net boarding
    count_tot$dispatch <- as.numeric(count_tot$dispatch) 
    count_tot$stnseq <- as.numeric(count_tot$stnseq)
    count_tot <- count_tot[order(count_tot$dispatch,count_tot$stnseq), ]# cumulative sequence
    count_tot$onboard <- ave(count_tot$difference, count_tot$dispatch, FUN=cumsum) #Calculate the number of onboard people

    btime <- aggregate(Time ~ dispatch + stnorder, data=record, min)
    colnames(btime) <- c("dispatch","stnorder","btime")

    result_tot <- merge(count_tot, btime, by.x=c("dispatch","stnseq"), by.y=c("dispatch","stnorder"), all=TRUE) #Add boarded time

    DepartureT <- record_board[, -c(3:7)]
    DepartureT <- subset(DepartureT, !duplicated(subset(DepartureT, select=c(Departure, dispatch))))
    result_tott <- (merge(result_tot, DepartureT, by = 'dispatch'))
    result_tott <- select(result_tott, "Departure","dispatch","stnseq","stnID","boardpeople","alightpeople","difference", "onboard", "btime")

    # Save result
    write.csv(result_tott, paste('Result2019/Occupancy_',as.character(line_ID),'_',line_NO,'.csv',sep="" ))
 } 
}

我还注意到,全局环境中的某些文件具有正确的data_frame,但由于某些原因其长度和大小为0。 关于此代码的任何评论将不胜感激〜

1 个答案:

答案 0 :(得分:1)

您应该执行for(i in nrow(buslist))

而不是for(i in 1: nrow(buslist))

nrow(buslist)为您提供一个数字,因此i取该值后,它将终止。相反,1:nrow(buslist)为您提供了可以循环通过的范围。