For loop problems in r: Error in if (length[i] == 1) { : missing value where TRUE/FALSE needed

前端 未结 1 917
野的像风
野的像风 2021-01-29 13:06
ann <- 1:2500
len <- sample(1:3,1000,replace=TRUE)
df <- data.frame(col1= c(1:2500),col2= c(1:2500))

for (i in 1:length(ann)) {
      if (length[i]==1) { 
             


        
相关标签:
1条回答
  • 2021-01-29 13:33

    Here we can iterate through all the elements of len, and for each do an inner loop that takes that element of len and enters it into that many rows of df$col1.

    df_row <- 1
    for (i in 1:length(len)) {
      for (j in 1:len[i]) {
        df[df_row, 1] = len[i]
        df_row <- df_row + 1
      }
    }
    
    0 讨论(0)
提交回复
热议问题