applying fct_recode

Hello, how can I apply fct_recode to the complete data.frame.
When using fct_recode, I can only apply it column by column.

my matrix (data frame) has 30 Variables (200 observations each). Some Variables are Numerical while others are Categorical. I want to transform all the Categorical (Yes/No) to 1 and 0.
Currently I am using:
recoding=c("1" = "Yes","0"="No")
fct_recode(mydata_cleaned$Var1, !!!recoding)

I want to be able to apply this to all the matrix as opposed to each Variables column individually.

thank you

library(tidyverse)
library(forcats)
inputdata <- data.frame (a=c("Yes","No","No"),
                         b=c("No","No","Yes"),
                         c=c("Yes","Yes","No"),stringsAsFactors = TRUE)
inputdata
#    a   b   c
# 1  Yes No Yes
# 2  No  No Yes
# 3  No Yes  No
final <- mutate_if(inputdata,
                   is.factor,
                   ~fct_recode(.,!!!c("1"="Yes","0"="No")))
final
#   a b c
# 1 1 0 1
# 2 0 0 1
# 3 0 1 0
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.