Hi, @shib, I think you are approaching. Just specify the data for this variable allyrs.
For here, in lazy way, I use .$allyrs but you can also use df$allyrs, I show it in the fold.
library(dplyr)
#>
#> 载入程辑包:'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
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))
df %>%
mutate_at(
paste(1997:2000)
,function(x) ifelse(.$allyrs == T, 1, 0)
)
#> # 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 1 1 1 TRUE
#> 4 0 0 0 0 FALSE
#> 5 0 0 0 0 FALSE
#> 6 1 1 1 1 TRUE
#> 7 0 0 0 0 FALSE
#> 8 1 1 1 1 TRUE
#> 9 1 1 1 1 TRUE
#> 10 0 0 0 0 FALSE
Created on 2018-11-03 by the reprex package (v0.2.1)
`The df$allyrs` way
library(dplyr)
#>
#> 载入程辑包:'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
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))
df %>%
mutate_at(
paste(1997:2000)
,function(x) ifelse(df$allyrs == T, 1, 0)
)
#> # 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 1 1 1 TRUE
#> 4 0 0 0 0 FALSE
#> 5 0 0 0 0 FALSE
#> 6 1 1 1 1 TRUE
#> 7 0 0 0 0 FALSE
#> 8 1 1 1 1 TRUE
#> 9 1 1 1 1 TRUE
#> 10 0 0 0 0 FALSE
Created on 2018-11-03 by the reprex package (v0.2.1)