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

Convert combination value into dummies in R

I have a dataset like this:

id<-c(1:6)
value<-c(" ","1", "1 6","1 777"," ", " ")
df<-data.frame(id, value)

Now I would like to convert it as dummy variable for each value, and use 0 and 1 to represent "yes" and "no". In other word, instead of count the combination value, i would like to count each value for each observation. for example, the first obs is NA, so only NA is yes, the third obs chose combo value "1" and "6", so in row no.3, the cols"1" and col"6" are marked as "1" (which is yes). Ideally get the table looks like this (plz ignore the dot after the numbers):

id 1 6 777 NA
1 0  0  0  1
2 1  0  0  0
3 1  1  0  0
4 1  0  1  0
5 0  0  0  1
6 0  0  0  1

i tried use package "fastdummies", my code is like this:

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

df<-dummy_cols(df,
             select_columns="value",
             split="")  

it does not work very well. Any solution for such case? Thanks a lot.

Also, when it spits out the dummy vars, the cols name like "value_", "value_6", is there any way to show the name as they were as value like "1", "6","777","NA". Thanks a lot~~!

>Solution :

We may need to convert the space elements to NA

library(dplyr)
library(fastDummies)
library(tidyr)
library(stringr)
df %>%
     na_if(" ") %>% 
     dummy_cols("value", split = " ", remove_selected_columns = TRUE) %>%  
     mutate(across(starts_with('value_'), replace_na, 0))  %>%
     rename_with(~ str_remove(.x, "value_"), starts_with("value_"))

-output

  id 1 6 777 NA
1  1 0 0   0  1
2  2 1 0   0  0
3  3 1 1   0  0
4  4 1 0   1  0
5  5 0 0   0  1
6  6 0 0   0  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