简体   繁体   English

for 循环中的 R 错误:意外的“=”并且无法识别“i”

[英]R error in for loop: unexpected '=' and not recognising 'i'

I have this loop, where X is a tibble with 6 columns and 3000 rows:我有这个循环,其中 X 是一个有 6 列和 3000 行的小标题:

for(i in 1:nrow(X)){if(X[i,6] = -999){
    X[i,6] <- NA
  }
} 

When I wrote the code, it was running perfectly, substituting all the -999 cells with NA.当我编写代码时,它运行完美,用 NA 替换了所有 -999 单元格。 However now it is returning this error:但是现在它返回此错误:

Error: unexpected '=' in "for(i in 1:nrow(daily_Geul)){if(daily_Geul[i,6] ="

and

Error in `[<-.tbl_df`(`*tmp*`, i, 6, value = NA) : object 'i' not found

I have also tried using '==' instead of '=', but I get the same error back.我也尝试过使用“==”而不是“=”,但我得到了同样的错误。 As for the second error, I am confused as i should be defined by the for loop.至于第二个错误,我很困惑,因为我应该由 for 循环定义。 I don't know how to correct these, as until yesterday the loop was working fine.我不知道如何纠正这些,因为直到昨天循环工作正常。

Thank you in advance for your help, as sorry if the issue is basic :)预先感谢您的帮助,如果问题是基本问题,我们很抱歉:)

try this:试试这个:

assuming X is your DataFrame and that your datatype in X[i,6] is not Character假设X是您的DataFrame并且您在X[i,6]中的数据类型不是Character

for(i in 1:nrow(X)){
    if(X[i,6] == -999){
    X[i,6] = NA
  }
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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