I have a data frame in R that is 89 columns wide, and 500,000 rows long. In each of the columns there are multiple 4 digit numeric codes, they can be in any column. I want to create a function that scans across each row to see if a code exists, if it does label as 1 if not 0, the new column must be named as the code searched for or something very similar (appended letter etc), rinse and repeat for ~450 such codes. Each new column would be labelled in some way after the code that was being searched for, like the 3669 column below.
c1 c2 c3 3369
1 2255 3669 NA 1
2 NA 5555 6598 0
3 NA NA 1245 0
I have attempted to do this using mutate, and rowSums see below, which works for an individual code, but I cannot get to work when using the sapply function. It just creates a single column called "x"
a <- function(x) {
SR2 <<- SR2 %>% mutate(x = ifelse(rowSums(SR2 == x, na.rm = TRUE) > 0, 1, 0))
}
The x in this function is a list of codes, so "3369", "2255" etc.
What am I missing here?