# To apply if else iterations on each row

Hi,

I want to use if else statement on each row with loops on given data frame.

ab = data.frame(x = c("P1","P2","P3","P4","P5"),
A = c("100","100","100","100","100"),
B= c("a","b","c","d","e"),
C= c("PR","PR","SR","SR","TR"),
D= c("75","125","NA","NA","NA"),
stringsAsFactors = FALSE)

Some conditions need to apply.I have used loop function but not working..

for(i in 1:nrow(ab)){
if((ab\$D != NULL ) & (ab\$C == PR) & (ab\$D > ab\$A))
{
ab\$New <- "0"

}
else if((ab\$D == NA) & (ab\$C == SR))
{
ab\$New <- "0"

}
else if((ab\$D == NA)&(ab\$C == TR))
{
ab\$New <- "1"

}
else
{
ab\$New<- "No"
}
}
Result should reflect in same data set with new column.
my conditions are:
1)If column d is not null and value of D is greater than column A and value of column c is PR then in new result column there should be print "0".
2)if column d is Null and value of column C is SR then in result column there should be "0".
3)if column d is null and value of column C is TR then in result column there must be "1"
4)Else print "NO"

Desired Output is :

x A B C D NEW
P2 100 b PR 125 0
P3 100 c SR 120 0
P4 100 d SR NA 0
P5. 100 e TR 50 1

Does the following code do what you want? Note that there is a difference between a variable having the value "NA" (which is a character string), it having an NA value (which will test TRUE with is.na()), and a variable being NULL.

``````library(dplyr)
ab = data.frame(x = c("P1","P2","P3","P4","P5"),
A = c("100","100","100","100","100"),
B= c("a","b","c","d","e"),
C= c("PR","PR","SR","SR","TR"),
D= c("75","125","NA","NA","NA"),
stringsAsFactors = FALSE)
ab
#>    x   A B  C   D
#> 1 P1 100 a PR  75
#> 2 P2 100 b PR 125
#> 3 P3 100 c SR  NA
#> 4 P4 100 d SR  NA
#> 5 P5 100 e TR  NA
ab <- ab %>% mutate(New = case_when(
D != "NA" & D > A & C == "PR" ~ "0",
D == "NA" & C == "SR" ~ "0",
D == "NA" & C == "TR" ~ "1",
TRUE ~ "NO"
))
ab
#>    x   A B  C   D New
#> 1 P1 100 a PR  75   0
#> 2 P2 100 b PR 125   0
#> 3 P3 100 c SR  NA   0
#> 4 P4 100 d SR  NA   0
#> 5 P5 100 e TR  NA   1
``````

Created on 2020-02-05 by the reprex package (v0.3.0)

1 Like

The function `ifelse()` is automatically vectorised:

Agreed with above comment on NA versus "NA" - so the command `b\$D != NULL` will always evaluate to TRUE in this example. Also, this argument will go wonky when using characters and not numbers: `ab\$D > ab\$A`. The argument `is.na()` seems to be betters at detecting true NA values.

``````ab = data.frame(
x = c("P1","P2","P3","P4","P5"),
A = c("100","100","100","100","100"),
B= c("a","b","c","d","e"),
C= c("PR","PR","SR","SR","TR"),
D= c("75","125", NA, NA, NA),
stringsAsFactors = FALSE)

ab\$New <- ifelse(!is.na(ab\$D)  & ab\$C == "PR" & ab\$D > ab\$A, "0",
ifelse(is.na(ab\$D) & ab\$C == "SR", "0",
ifelse(is.na(ab\$D) & ab\$C == "TR", "1", "No")))
``````

Hi,

Thanks...its working and it is a very simple solution.

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.