Home > front end >  How to search for a value in multiple columns in R?
How to search for a value in multiple columns in R?

Time:06-26

I need to search through multiple columns. Then, if any of the values in the columns equals a condition, then I need to extract the value in the column to the left of the column searched.

Below is a sample of my code. I am searching the congress_1 and congress_2 columns for the value 108. If there are any such matches, then I need to extract the value in position_1 and position_2 respectively.

a2 <- data.frame(position_1 = c("Senator", "Rep", "Senator", "Rep"),
                 congress_1 = c(107, 107, 108, 109),
                 position_2 = c("Senator", "Rep", "Senator", "Rep"),
                 congress_2 = c(108, 108, 109, 110))

I would like the output to look something like this:

row position_108
1   "Senator"
2   "Senator"
3   "Rep"
4   "none"

I've been trying to use the which() function, but the issue is that I have a large data frame and there are many columns that I need to searched through. Because of the nature of my question, I do think that I need to use the ifelse() function. The challenge I've been having is how to extract the data to the left of the column if there is a match.

Any help is greatly appreciated!

CodePudding user response:

Does transmute with case_when do what you need?

library(dplyr)
a2 %>%
  transmute(answer = case_when(congress_1 == 108 ~ position_1,
                               congress_2 == 108 ~ position_2))

   answer
1 Senator
2     Rep
3 Senator
4    <NA>

CodePudding user response:

You can use sapply with if

position_108 <- sapply(1:nrow(a2) , \(x) if(a2$congress_1[x] == 108)
    a2$position_1[x] else if(a2$congress_2[x] == 108)
        a2$position_1[x] else "none")

ans <- data.frame(position_108)

ans

#>  position_108
#> 1      Senator
#> 2          Rep
#> 3      Senator
#> 4         none
  • Related