xhr489
xhr489

Reputation: 2309

Dummy columns if row contains string

I have a data set that could look like this:

x <- data.frame(id=c(1,2,3), 
               col1=c("UX1", "UX3", "UX2"),
               col2=c("UX2", "UX1", "UX1"),
               col3=c("PROC1", "PROC2", "PROC3"),
               col4=c("PROC3", "PROC3", "PROC1")
               )

output:

  id col1 col2  col3  col4
1  1  UX1  UX2 PROC1 PROC3
2  2  UX3  UX1 PROC2 PROC3
3  3  UX2  UX1 PROC3 PROC1

and I would like the output to look like this:

x2 <- data.frame(id=c(1,2,3),  
           col1=c("UX1", "UX3", "UX2"),
           col2=c("UX2", "UX1", "UX1"),
           col3=c("PROC1", "PROC2", "PROC3"),
           col43=c("PROC3", "PROC3", "PROC1"),

           UX1=c(1,1,1),
           UX2=c(1,0,1),
           UX3=c(0,1, 0),
           PROC1 =c(1,0,1),
           PROC2=c(0,1,0),
           PROC3 = c(1,1,1))

Wanted output:

  id col1 col2  col3 col43 UX1 UX2 UX3 PROC1 PROC2 PROC3
1  1  UX1  UX2 PROC1 PROC3   1   1   0     1     0     1
2  2  UX3  UX1 PROC2 PROC3   1   0   1     0     1     1
3  3  UX2  UX1 PROC3 PROC1   1   1   0     1     0     1

So basicalle to create a dummy if a row contains a string. I can create dummy.data.frame using library(dummies) e.g.

y <- dummy.data.frame(x)

but this approch thinks that (for example) UX1 in column one is different than UX1 in column two. So dummy.data.frame does not work...

Upvotes: 0

Views: 419

Answers (2)

Sotos
Sotos

Reputation: 51582

Here is an idea via tidyverse. We first gather all except the id variable. We then spread to get the required structure and use a simply replace to 'dummify' our data, i.e.

library(tidyverse)

x %>% 
 gather(var, val, -id) %>% 
 spread(val, var, fill = 0) %>% 
 mutate_at(vars(-id), funs(replace(., . != 0, 1)))

which gives,

  id PROC1 PROC2 PROC3 UX1 UX2 UX3
1  1     1     0     1   1   1   0
2  2     0     1     1   1   0   1
3  3     1     0     1   1   1   0

You can then very easily cbind() to the original data frame, i.e.

x2 <- x %>% 
  gather(var, val, -id) %>% 
  spread(val, var, fill = 0) %>% 
  mutate_at(vars(-id), funs(replace(., . != 0, 1)))

cbind(x, x2)
#   id proc1 proc2 proc3 proc4 id PROC1 PROC2 PROC3 UX1 UX2 UX3
#1  1   UX1   UX2 PROC1 PROC3  1     1     0     1   1   1   0
#2  2   UX3   UX1 PROC2 PROC3  2     0     1     1   1   0   1
#3  3   UX2   UX1 PROC3 PROC1  3     1     0     1   1   1   0

NOTE: As @mmn points out, we can merge instead of cbind, i.e.

x %>%
  gather(var, val, - id) %>%
  spread(val, var, fill = 0) %>%
  mutate_at(vars(-id), funs(replace(., . != 0, 1))) %>%
  left_join(x, ., by = 'id')

#  id col1 col2  col3  col4 PROC1 PROC2 PROC3 UX1 UX2 UX3
#1  1  UX1  UX2 PROC1 PROC3     1     0     1   1   1   0
#2  2  UX3  UX1 PROC2 PROC3     0     1     1   1   0   1
#3  3  UX2  UX1 PROC3 PROC1     1     0     1   1   1   0

Upvotes: 2

ira
ira

Reputation: 2644

Just for completeness, suggesting also a data.table alternative:

# load the data table package
library(data.table)

# create the sample data set
x <- data.frame(id=c(1,2,3), 
                col1=c("UX1", "UX3", "UX2"),
                col2=c("UX2", "UX1", "UX1"),
                col3=c("PROC1", "PROC2", "PROC3"),
                col4=c("PROC3", "PROC3", "PROC1")
)

# convert data frame to data table
x <- data.table(x)

# first convert data to long format using melt function
# then use cast to go back to wide format, convert "value" variable to columns and check where are missing values
# then join on the original data set
x[dcast(melt(x, "id"), id ~ value, fun = function(x) sum(!is.na(x))), on = "id"]

Upvotes: 0

Related Questions