[英]Based on the value in one column change the value in another column
I created a table "examres" like this 我这样创建了一个表格“检查”
names marks result
1 dinesh 60 pass
2 aiysha 70 pass
3 ravi 40 fail
4 rajesh 55 pass
5 achyuthy 80 pass
6 snigdha 30 fail
7 mounica 0 pass
8 55 pass
9 0 fail
10 mourya 0 pass
11 deepa sinde 25
12 hima sekhar 55 pass
13 30 fail
14 dhatri 60
In the above table I want to change the result column based on the "marks" column and my condition is marks<50 "fail" else "pass" 在上表中,我想基于“标记”列更改结果列,我的条件是标记<50“失败”,否则为“合格”
I used: 我用了:
ifelse(examres$marks<50,examres$result<-"fail",examres$result<-"pass")
but its not working. 但它不起作用。
It was almost correct, try this: 几乎是正确的,请尝试以下操作:
examres$result <- ifelse(examres$marks<50,"fail","pass")
ifelse - Description
ifelse-说明
ifelse returns a value with the same shape as test which is filled with elements selected from either yes or no depending on whether the element of test is TRUE or FALSE.
ifelse返回与测试形状相同的值,其中填充有从是或否中选择的元素,具体取决于测试元素是TRUE还是FALSE。
Usage
用法
ifelse(test, yes, no)ifelse(测试,是,否)
To change names to NA when it is blank - ""
: 要将名称更改为NA(空白)-
""
:
examres$names[examres$names == ""] <- NA
Note, use na.strings
options when reading in the file . 请注意,在读取文件时,请使用
na.strings
选项。 Then we would avoid NA problem altogether. 然后,我们将完全避免NA问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.