虚拟列(如果行包含字符串)

时间:2018-12-17 13:09:05

标签: r dummy-variable

我有一个看起来像这样的数据集:

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")
               )

输出:

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

,我希望输出看起来像这样:

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))

想要的输出:

  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

因此,如果一行包含字符串,则创建一个虚拟对象的基本规则。我可以使用dummy.data.frame创建library(dummies),例如

y <- dummy.data.frame(x)

,但是此方法认为(例如)第一列中的UX1与第二列中的UX1不同。因此dummy.data.frame无法正常工作...

2 个答案:

答案 0 :(得分:2)

这是一个通过tidyverse的想法。我们首先gather个变量,id除外。然后,我们spread得到所需的结构,并使用简单的replace“仿制”我们的数据,即

library(tidyverse)

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

给出,

  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

然后您可以非常容易地cbind()到原始数据帧,即

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

注意:正如@mmn所指出的,我们可以merge代替cbind,即

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

答案 1 :(得分:0)

仅出于完整性考虑,还建议使用data.table替代方法:

# 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"]