Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

a proper function to convert some factors in a dataset into numeric variables

I have this dataset that includes all factors

questions = data.frame(sex = c(rep("M",30),rep("F",30)),
                       do_you_like_playing_football = rep(c("yes","no","dk"),20),
                       do_you_like_playing_basketball = rep(c("yes","no","dk"),20))

questions = questions%>%mutate_if(is.character,factor)

I want to convert the second and the third variables into numeric variables (yes=1, no=5, dk=8) while preserving the dataset format.

what a proper algorithm to do that ? thanks.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You could do the following:

cols = c("do_you_like_playing_football", "do_you_like_playing_basketball")
lut  = c(yes=1, no=5, dk=8)

questions[cols] = lapply(questions[cols], \(x) lut[x])

Or more concisely with magrittr

library(magrittr)
questions[cols] %<>% lapply(\(x) lut[x])

Result

head(questions)
#   sex do_you_like_playing_football do_you_like_playing_basketball
# 1   M                            8                              8
# 2   M                            5                              5
# 3   M                            1                              1
# 4   M                            8                              8
# 5   M                            5                              5
# 6   M                            1                              1
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading