How to check for a value in multiple columns( finding array equivalent in SAS)?

Hi All,

I have a large dataframe where I want to filter for a value "Y" , if present in multiple columns.
If so, then I will have to create a new character column and assign value 1 there otherwise, need to assign blank where the condition doesn't match. NB: the columns are not next to each other neither they carry any common string in their names. As a SAS professional I'm familiarized with arrays to solve this and as a beginner in R, would really like to explore the methods to approach this problem. Would really appreciate if, one can guide please. Thanks!!

Below may be further modified to convert "0" to the empty string ("", not recommended) or NA.

toy <- data.frame(a = c("A","Y"), b = c("B","X"))
toy["is_Y"] <- as.character(rowSums(toy == "Y"))
toy
#>   a b is_Y
#> 1 A B    0
#> 2 Y X    1

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.