Richard Packer
Richard Packer

Reputation: 21

Mutate a column and name it after the input variable for a function in R

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?

Upvotes: 0

Views: 189

Answers (1)

A. Suliman
A. Suliman

Reputation: 13125

Use quo_name with !! to get the correct column name. Use map_dfc to get the output in data frame

library(purrr)
library(dplyr)
df_out <- map_dfc(c('2255','5555'), 
                  ~transmute(df,!!quo_name(.x) := ifelse(rowSums(df == .x, na.rm = TRUE) > 0, 1, 0)))

bind_cols(df,df_out)

Data

df <- structure(list(c1 = c(2255L, NA, NA), c2 = c(3669L, 5555L, NA), c3 = c(NA, 6598L, 1245L), 
         `3369` = c(1L, 0L, 0L)), class = "data.frame", row.names = c("1", "2", "3")) 

Upvotes: 1

Related Questions