简体   繁体   English

R中的IF / THEN / ELSE

[英]IF/THEN/ELSE in R

everyone. 大家。

Hopefully an easy syntax question. 希望这是一个简单的语法问题。 I'm trying to create a new variable in a table in R which would say "1" if my patient was in the age range I was looking at, or "0" for no. 我正在尝试在R中的表中创建一个新变量,如果我的患者处于我正在查看的年龄范围内,它将显示为“ 1”,否则为“ 0”。 The age range I'm interested is between 2-155. 我感兴趣的年龄范围是2-155。 The code is running without any errors, but it is not working. 该代码正在运行,没有任何错误,但无法正常工作。 When I look in my table, the new variable will say 1 even though the age4 is 158 Here is what I have: 当我查看表格时,即使age4为158,新变量也会显示1。

table$newvar <- if (table$age4>=2 && table$age4 <=155) {table$newvar=1} else {table$newvar=0}

Any help is appreciated! 任何帮助表示赞赏! Thanks in advance! 提前致谢!

Two changes should be made: 应该进行两个更改:

  1. Use the vectorized ifelse() function to generate the new column data. 使用向量化的ifelse()函数生成新的列数据。
  2. Use the vectorized & logical-AND operator when combining the results of the comparisons. 合并比较结果时,请使用向量化&逻辑与运算符。

table <- data.frame(age4=seq(1,200,10));
table$newvar <- ifelse(table$age4>=2 & table$age4<=155,1,0);
table;
##    age4 newvar
## 1     1      0
## 2    11      1
## 3    21      1
## 4    31      1
## 5    41      1
## 6    51      1
## 7    61      1
## 8    71      1
## 9    81      1
## 10   91      1
## 11  101      1
## 12  111      1
## 13  121      1
## 14  131      1
## 15  141      1
## 16  151      1
## 17  161      0
## 18  171      0
## 19  181      0
## 20  191      0

The reason your code is not working is because the if statement and the && operator are not vectorized. 您的代码无法正常工作的原因是因为if语句和&&运算符未向量化。 The && operator only examines the first element of each operand vector, and only returns a one-element vector representing the result of the logical-AND on those two input values. &&运算符仅检查每个操作数向量的第一个元素,并且仅返回表示这两个输入值上逻辑与结果的单元素向量。 The if statement always expects a one-element vector for its conditional, and executes the if-branch if that element is true, or the else-branch if false. if语句始终期望一个元素向量作为其条件,如果该元素为true,则执行if-branch;如果为false,则执行else-branch。

If you use a multiple-element vector as the conditional in the if statement, you get a warning: 如果在if语句中使用多元素向量作为条件,则会收到警告:

if (c(T,F)) 1 else 0;
## [1] 1
## Warning message:
## In if (c(T, F)) 1 else 0 :
##   the condition has length > 1 and only the first element will be used

But for some odd reason, you don't get a warning if you use a multiple-element vector as an operand to && (or || ): 但是出于某种奇怪的原因,如果使用多元素向量作为&& (或|| )的操作数, 则不会收到警告:

c(T,F) && c(T,F);
## [1] TRUE

That's why your code appeared to succeed (by which I mean it didn't print any warning message), but it didn't actually do what was intended. 这就是为什么您的代码似乎成功的原因(我的意思是它没有显示任何警告消息),但实际上并未执行预期的操作。

When used in arithmetic TRUE and FALSE become 1 and 0 so: 在算术运算中使用TRUEFALSE10因此:

transform(table, newvar = (age4 >= 2) * (age4 <= 155) )

These also work: 这些也可以工作:

transform(table, newvar = as.numeric( (age4 >= 2) & (age4 <= 155) ) )

transform(table, newvar = ( (age4 >= 2) & (age4 <= 155) ) + 0 )

transform(table, newvar = ifelse( (age4 >= 2) & (age4 <= 155), 1, 0) )

transform(table, newvar = (age4 %in% 2:155) + 0) # assuming no fractional ages

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

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