R中的if语句:尽管存在值,但仍存在”缺少值”错误

if-statement in R: “missing value” error despite existing value

如果存在完美的健康值,则if语句返回"缺少值"错误。

我想编写一个简单的脚本来删除数据集中的行(如果其中一个条目包含某个标记)。我在新列(containsMR)中分配了一个指标变量,然后使用for循环遍历各行。如果指示符为TRUE,则应删除该行。

到目前为止,指示器已正确分配,效果很好。有趣的部分:在循环的if语句中似乎无法读取值,因为它返回" if(data(containsMR [i]){
缺少需要TRUE / FALSE的值"。

给出正确(且完整)的指标变量分配,这让我感到惊讶。更奇怪的是:删除了一些但不是全部带有正指示符的行(使用打印输出和table(data $ containsMR)进行了检查)。

现在真的很奇怪:如果我再运行一次相同的循环,它将删除其余的列(应有的话),但是返回相同的错误。因此,从理论上讲,我可以运行两次循环,忽略错误并获得想要的结果。那真的不是我在做什么的重点。

修正的修正:
-从for改为while循环
-将指标(和if语句)更改为整数(0,1)
-在RStudio和R控制台中运行脚本
-更改了变量名称,包括了/排除了定义(例如,添加代理变量row_number而不是一行调用它。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Script to delete all rows containing"MR" in column"EXAM_CODE"

# import file
data <- read.csv("C:\\\\ScriptingTest\\\\ablations 0114.csv")

# add indicator column
for (i in 1:nrow(data)){
    data$containsMR[i] <- ifelse(grepl("MR", toString(data$EXAM_CODE[i])), TRUE, FALSE)
}

# remove rows with positive indicator
row_number <- nrow(data)
for (i in 1:row_number){
    if (data$containsMR[i]){
        data <- data[-c(i),]
    }
}

# export csv
write.csv(data,"C:\\\\ScriptingTest\\\\export.csv")


为说明该问题正在修改要循环的for循环中对象的大小,请参见以下示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
n <- nrow(mtcars)

for (i in 1:n){
  cat("\
 mtcars currently has",nrow(mtcars),"rows;","accessing row",i)
  if (mtcars$cyl[i] == 4){
    mtcars <- mtcars[-i,]
  }
}

> mtcars currently has 32 rows; accessing row 1
 mtcars currently has 32 rows; accessing row 2
 mtcars currently has 32 rows; accessing row 3
 mtcars currently has 31 rows; accessing row 4
 mtcars currently has 31 rows; accessing row 5
 mtcars currently has 31 rows; accessing row 6
 mtcars currently has 31 rows; accessing row 7
 mtcars currently has 30 rows; accessing row 8
 mtcars currently has 30 rows; accessing row 9
 mtcars currently has 30 rows; accessing row 10
 mtcars currently has 30 rows; accessing row 11
 mtcars currently has 30 rows; accessing row 12
 mtcars currently has 30 rows; accessing row 13
 mtcars currently has 30 rows; accessing row 14
 mtcars currently has 30 rows; accessing row 15
 mtcars currently has 30 rows; accessing row 16
 mtcars currently has 29 rows; accessing row 17
 mtcars currently has 28 rows; accessing row 18
 mtcars currently has 28 rows; accessing row 19
 mtcars currently has 28 rows; accessing row 20
 mtcars currently has 28 rows; accessing row 21
 mtcars currently has 28 rows; accessing row 22
 mtcars currently has 27 rows; accessing row 23
 mtcars currently has 26 rows; accessing row 24
 mtcars currently has 26 rows; accessing row 25
 mtcars currently has 26 rows; accessing row 26
 mtcars currently has 25 rows; accessing row 27
Error in if (mtcars$cyl[i] == 4) { :
  missing value where TRUE/FALSE needed


您也许可以将其简化为

1
newdata <-  data[!grepl("MR", data$EXAM_CODE),]