I am new to R and may be this is a silly question, just wanted to check if multiple loops condition works with vector.
I have a csv file imported as data and I am trying to code something like if column = R then 1 elseif column = A then 2 else column = G then 3 in one column. I can successfully create it in three separate columns but unable to include condition in one.
This gives me an warning "Warning message: In if (DataIfElse$Cost == "R") { : the condition has length > 1 and only the first element will be used" and only updates first value in the column with desired result. Can anyone please help me resolve the same.
The vectorized form of if is ifelse and you could chain those together. However, the dplyr package has the function case_when which uses a convenient syntax for effectively chaining together ifelse calls.
library(dplyr)
DF <- data.frame(Cost = c("X", "R", "Y", "T"),
Schedule = c("A", "T", "X", "U"),
Scope = c("X", "T", "G", "Q"),
stringsAsFactors = FALSE)
DF
#> Cost Schedule Scope
#> 1 X A X
#> 2 R T T
#> 3 Y X G
#> 4 T U Q
DF <- mutate(DF, P =
case_when(
Cost == "R" | Schedule == "R" | Scope == "R" ~ "R",
Cost == "A" | Schedule == "A" | Scope == "A" ~ "A",
Cost == "G" | Schedule == "G" | Scope == "G" ~ "G",
TRUE ~ "Z"
)
)
DF
#> Cost Schedule Scope P
#> 1 X A X A
#> 2 R T T R
#> 3 Y X G G
#> 4 T U Q Z
I tried including the code and it worked, but the output is the same, it considers first value and provides entire column with the same value. Am I doing something wrong here! Thanks
You are not using the same code as FJCC suggested. You have changed | to ||, and used Df$... instead of just column name inside case_when. You need not give dataframe name inside case_when.
Also, | and || are not same. The longer reports only one boolean based on 1st element, and the shorter returns a vector of booleans. I posted regarding this a while back, so I'm linking that below.