# Recoding continuous variables to binary

Hi! I have about 100 variables that i would like to dichotomise into, either presence (1) or absence (0) . I found some examples, but they were using the funs argument, which was deprecated. I tried using the list argument but i get an error message.

``````library(tidyverse)

var_a <- c(0,0,1,3,2,1,3,2,2,0,0,0)
var_a2 <- c(0,1,3,2,0, 0,3,1,2,0,0,0)
var_a3 <- c(0,0,3,0,3,1,2,2,1,0,1,0)
id <- c(1,2,3,4,5,6,7,8,9,10, 11, 12)

df1<- data.frame(var_a,var_a2, var_a3, id )

str(df1)

df1 <- df1 %>%
mutate_at(
vars(starts_with('var_')),
list(case_when(
. == 0 ~ 0,
. > 0 ~ 1)))

``````

"Error in `mutate_at()`:
! `.funs` must be a one sided formula, a function, or a function name.
Backtrace:

1. df1 %>% ...
2. dplyr::mutate_at(...)"

you may try to replace "list" with "~".

``````library(tidyverse)

var_a <- c(0,0,1,3,2,1,3,2,2,0,0,0)
var_a2 <- c(0,1,3,2,0, 0,3,1,2,0,0,0)
var_a3 <- c(0,0,3,0,3,1,2,2,1,0,1,0)
id <- c(1,2,3,4,5,6,7,8,9,10, 11, 12)

df1<- data.frame(var_a,var_a2, var_a3, id )

df1 %>%
mutate_at(
vars(starts_with('var_')),
~(case_when(
. == 0 ~ 0,
. > 0 ~ 1)))
#
#     var_a var_a2 var_a3 id
# 1      0      0      0  1
# 2      0      1      0  2
# 3      1      1      1  3
# 4      1      1      0  4
# 5      1      0      1  5
# 6      1      0      1  6
# 7      1      1      1  7
# 8      1      1      1  8
# 9      1      1      1  9
# 10     0      0      0 10
# 11     0      0      1 11
# 12     0      0      0 12
``````

This topic was automatically closed 7 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.