Is there a single-call way to assign several specific columns to a value using dplyr, based on a condition from a column outside that group of columns?
My issue is that mutate_if
checks for conditions on the specific columns themselves, and mutate_at
seems to limit all references to just those same specific columns. Whereas I want to mutate based on a corresponding value in a column outside the ones I specify with .vars
.
library(dplyr)
df <- structure(list(`1997` = c(0, 0, 1, 0, 0, 0, 0, 0, 0, 0),
`1998` = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
`1999` = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
`2000` = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
allyrs = c(FALSE, FALSE, TRUE, FALSE,
FALSE, TRUE, FALSE, TRUE, TRUE, FALSE)),
class = c("tbl_df", "tbl", "data.frame"),
row.names = c(NA, -10L))
# What is the condensed, multi-column mutate, dplyr equivalent to
df[df$allyrs == T,]$`1997` <- 1
df[df$allyrs == T,]$`1998` <- 1
df[df$allyrs == T,]$`1999` <- 1
df[df$allyrs == T,]$`2000` <- 1
# In theory I just want to use mutate_if or mutate_at,
# but dplyr::mutate_if() checks conditions on the column itself, not a neighboring column,
# like checking if a column is a numeric or character'
# In theory I would use mutate_at(), but specifying which columns I want to modify makes it such
# that dplyr can no longer find the column I want to check conditions on
df %>% mutate(`1997` = ifelse(allyrs == T, 1, `1997`))
#> # A tibble: 10 x 5
#> `1997` `1998` `1999` `2000` allyrs
#> <dbl> <dbl> <dbl> <dbl> <lgl>
#> 1 0 0 0 0 FALSE
#> 2 0 0 0 0 FALSE
#> 3 1 0 0 0 TRUE
#> 4 0 0 0 0 FALSE
#> 5 0 0 0 0 FALSE
#> 6 1 0 0 0 TRUE
#> ....
# repeat for all other years....
df %>% mutate_at(paste(1997:2000), function(x) 1)
#> # A tibble: 10 x 5
#> `1997` `1998` `1999` `2000` allyrs
#> <dbl> <dbl> <dbl> <dbl> <lgl>
#> 1 1 1 1 1 FALSE
#> 2 1 1 1 1 FALSE
#> ....
# mutate_at works insofar as you can select multiple columns but...
df %>% mutate_at(paste(1997:2000), function(x) ifelse(allyrs == T, 1, 0))
#> Error in ifelse(allyrs == T, 1, 0): object 'allyrs' not found
# its usage constrains available variables to check
#> Error in ifelse(allyrs == T, 1, 0) : object 'allyrs' not found
# Is there just not a way to do this in one call using dplyr?