【发布时间】:2017-09-21 21:50:15
【问题描述】:
我有一个数据框,其中 id 可以有多种事件类型
> head(eventtype, 10)
id event_type
1 6597 event_type 11
2 8011 event_type 15
3 2597 event_type 15
4 5022 event_type 15
5 5022 event_type 11
6 6852 event_type 11
7 6852 event_type 15
8 5611 event_type 15
9 14838 event_type 15
10 14838 event_type 11
我想转成格式
id event_type 1 event_type 2 event_type 3 ... event_type 50
14838 0 0 0 ... 0
在 R 中实现这一目标的最佳方法是什么?有包吗? 我尝试过使用假人:
new_my_data
但它不起作用。我也尝试搜索,但可以找到解决此特定问题的方法。几乎所有的帖子都假设一种热编码是众所周知的。
请帮忙。
【问题讨论】:
-
还有一个叫做 caret 的包,你可以使用 dummyVars 来创建虚拟变量。 inclass.kaggle.com/c/15-071x-the-analytics-edge-summer-2015/….
-
library(tidyverse); df %>% mutate(i = 1) %>% spread(event_type, i, fill = 0) -
@alistaire 谢谢,它完成了这项工作:) 但你的意思是 library(tidyr) 和 library(dplyr) 而不是 tidyverse 吗??
标签: r dataframe one-hot-encoding