for循环不会rbind()矢量

我需要有关此for循环的帮助。它似乎正在运行,没有任何错误,但是最后它没有为每次迭代将向量“ row.toadd”插入数据框中。但是,当我在for循环循环后编写rbind()函数时,它将绑定在循环中创建的最后一个向量,并将其插入数据帧。但是我希望它在for循环中的每次迭代中都这样做!当我编写“ row.toadd”时,就像我想要的那样,有一个用于最后一次迭代的向量,所以它是最后一部分不起作用。我认为这真的很奇怪,因为我之前使用过类似的代码并且它有效。

  for(i in 1:length(event.files)){
event.temp <- fromJSON(file=event.files[i])

shot.index <- which(unlist(lapply(event.temp,function(x) x$type$name))=="Shot")


shots.France <- shot.index[which(unlist(lapply(shot.index,function(x) event.temp[[x]]$team$name))=="France")]

shots.France.df <- data.frame(matrix(NA,nrow=1,ncol=11))
colnames(shots.France.df) <- c("Possession","Player","X.Shot","Y.Shot",
                              "Shot.Type","xG","keypassid","X.KeyPass","Y.KeyPass","Shot.Outcome","Shot.Foot")



if(length(shots.France)!=0){
  for(p in 1:length(shots.France)){
    shots.France.temp <- event.temp[[shots.France[p]]]
    possession <- shots.France.temp$possession
    shooter <- shots.France.temp$player$name
    shots.location <- shots.France.temp$location
    shots.type <- shots.France.temp$shot$technique$name
    shots.xg <- ifelse("statsbomb_xg" %!in% names(shots.France.temp$shot),NA, shots.France.temp$shot$statsbomb_xg)
    keypass <- ifelse("key_pass_id" %!in% names(shots.France.temp$shot),NA,shots.France.temp$shot$key_pass_id)
    keypass.location <- if(!is.na(keypass)){
      as.vector(unlist(pass.team1.df[which(pass.team1.df$passid==keypass),c("X.Pass","Y.Pass")]))
    }else{
      c(NA,NA)
    }
    shots.outcome <- shots.France.temp$shot$outcome$name
    body.part <- shots.France.temp$shot$body_part$name

    row.toadd <- c(possession,shooter,shots.location,shots.type,shots.xg,keypass,keypass.location[1],keypass.location[2],shots.outcome,body.part)

    shots.France[p,] <- row.toadd 
    shots.France.df <- rbind(shots.France.df,row.toadd)
    shots.France.list <- list(shots.France.df)

  }
}
}

评论