Reconstruct a categorical variable from dummies in R

允我心安 提交于 2020-01-30 03:21:32

问题


Heyho, I am a beginner in R and have a problem to which I couldn't find a solution so far. I would like to transform dummy variables back to categorical variables.

|dummy1| dummy2|dummy3|
|------| ------|------|
| 0    | 1     |0     |
| 1    | 0     |0     |
| 0    | 1     |0     |
| 0    | 0     |1     |

into:

|dummy |
|------|
|dummy2|
|dummy1|
|dummy2|
|dummy3|

Do you have any idea how to do that in R? Thanks in advance.


回答1:


You can do this with data.table

id_cols = c("x1", "x2") 
data.table::melt.data.table(data = dt, id.vars = id_cols, 
                            na.rm = TRUE, 
                            measure = patterns("dummy"))

Example:

t = data.table(dummy_a = c(1, 0, 0), dummy_b = c(0, 1, 0), dummy_c = c(0, 0, 1), id = c(1, 2, 3))
data.table::melt.data.table(data = t, 
                            id.vars = "id", 
                            measure = patterns("dummy_"), 
                            na.rm = T)[value == 1, .(id, variable)]

Output

   id variable
1:  1  dummy_a
2:  2  dummy_b
3:  3  dummy_c

It's even easier if you remplaze 0 by NA, so na.rm = TRUE in melt will drop every row with NA




回答2:


We can use max.col

data.frame(dummy = names(df1)[max.col(df1)])
#    dummy
#1 dummy2
#2 dummy1
#3 dummy2
#4 dummy3

data

df1 <- structure(list(dummy1 = c(0L, 1L, 0L, 0L), dummy2 = c(1L, 0L, 
 1L, 0L), dummy3 = c(0L, 0L, 0L, 1L)), .Names = c("dummy1", "dummy2", 
 "dummy3"), class = "data.frame", row.names = c(NA, -4L))



回答3:


Here is a tidyverse solution, using tidyr::gather. Here we treat the key as the variable that each dummy is a category of, and value as the presence/absence. Replacing 0 with NA combined with na.rm = TRUE in gather means we don't keep all the rest of the rows we don't want and don't create an unnecessarily large intermediate dataset.

df1 <- structure(list(dummy1 = c(0L, 1L, 0L, 0L), dummy2 = c(1L, 0L, 
                                                             1L, 0L), dummy3 = c(0L, 0L, 0L, 1L), ed1 = c(1, 0, 1, 0), ed2 = c(0, 
                                                                                                                               1, 0, 1), id = c(1, 2, 3, 4)), .Names = c("dummy1", "dummy2", 
                                                                                                                                                                         "dummy3", "ed1", "ed2", "id"), row.names = c(NA, -4L), class = "data.frame")
library(tidyverse)
df1 %>%
  mutate_at(vars(dummy1:dummy3, ed1:ed2), ~ ifelse(. == 0, NA, .)) %>%
  gather("dummy", "present", dummy1:dummy3, na.rm = TRUE) %>%
  gather("ed", "present2", ed1:ed2, na.rm = TRUE) %>%
  select(-present, -present2)
#>   id  dummy  ed
#> 2  1 dummy2 ed1
#> 3  3 dummy2 ed1
#> 5  2 dummy1 ed2
#> 8  4 dummy3 ed2

Created on 2018-03-06 by the reprex package (v0.2.0).



来源:https://stackoverflow.com/questions/49130366/reconstruct-a-categorical-variable-from-dummies-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!