Dinesh
Dinesh

Reputation: 239

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"

I used:

ifelse(examres$marks<50,examres$result<-"fail",examres$result<-"pass")

but its not working.

Upvotes: 2

Views: 3390

Answers (1)

zx8754
zx8754

Reputation: 56004

It was almost correct, try this:

examres$result <- ifelse(examres$marks<50,"fail","pass")

ifelse - Description

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.

Usage
ifelse(test, yes, no)

To change names to NA when it is blank - "":

examres$names[examres$names == ""] <- NA

Note, use na.strings options when reading in the file. Then we would avoid NA problem altogether.

Upvotes: 3

Related Questions