Home > Enterprise >  How to loop multiple columns in R
How to loop multiple columns in R

Time:07-01

I have data frame which I have created in R using below code:

test_data <- data.frame(
  id = c(1:10),
  Test1 = c(NA, NA, F, F, T, T, T, NA, F, F),
  Test2 = c(NA, NA, NA, F, F, F, F, T, T, T)
)
test_data

I want an output such that if Test1 = T then "A", else if Test2 = T then "B" else "C".

I have tried using ifelse() condition but not able to get correct output.

test_data$test <- ifelse(test_data$Test1 == TRUE, "A", 
                                    ifelse(test_data$Test2 == TRUE, "B", 'C'))
table(test_data$test)

Is there any way I can use for loop for 2 columns in R like how we do in list comprehension in Python or use zip() function with for loop?

CodePudding user response:

A possible solution:

library(dplyr)

test_data %>% 
  mutate(test = case_when(Test1 == T ~ "A",
                          Test2 == T ~ "B",
                          TRUE ~ "C"))
#>    id Test1 Test2 test
#> 1   1    NA    NA    C
#> 2   2    NA    NA    C
#> 3   3 FALSE    NA    C
#> 4   4 FALSE FALSE    C
#> 5   5  TRUE FALSE    A
#> 6   6  TRUE FALSE    A
#> 7   7  TRUE FALSE    A
#> 8   8    NA  TRUE    B
#> 9   9 FALSE  TRUE    B
#> 10 10 FALSE  TRUE    B

CodePudding user response:

The issue is that the NA values return NA, and not FALSE. You can do something like this to handle them.

ifelse(!test_data$Test1 %in% c(FALSE, NA), "A", 
       ifelse(!test_data$Test2 %in% c(FALSE, NA), "B", 'C'))

Or to make life a bit easier, create a function.

onlyTRUE <- function(x) replace(x, is.na(x), FALSE) == TRUE

ifelse(onlyTRUE(test_data$Test1), "A", 
       ifelse(onlyTRUE(test_data$Test2), "B", 'C'))
  •  Tags:  
  • r
  • Related