列/变量的虚拟化 [英] Dummyfication of a column/variable

查看:160
本文介绍了列/变量的虚拟化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在R中设计一个神经网络。为此,我必须准备数据并导入表格。

I'm designing a neural Network in R. For that I have to prepare my data and have imported a table.

例如:

      time    hour Money day
1:  20000616    1  9.35   5
2:  20000616    2  6.22   5 
3:  20000616    3  10.65  5
4:  20000616    4  11.42  5
5:  20000616    5  10.12  5
6:  20000616    6  7.32   5

现在我需要进行身份验证。我的决赛桌应该像这样:

Now I need a dummyfication. My final table should look like this:

      time    Money day  1   2   3   4   5   6   
1:  20000616  9.35   5   1   0   0   0   0   0
2:  20000616  6.22   5   0   1   0   0   0   0
3:  20000616  10.65  5   0   0   1   0   0   0
4:  20000616  11.42  5   0   0   0   1   0   0
5:  20000616  10.12  5   0   0   0   0   1   0
6:  20000616  7.32   5   0   0   0   0   0   1

是否有一种简单的方法/智能方法将我的桌子转换为新的布局?
还是以编程方式在R中?我需要在R中而不是在导入之前执行此操作。

Is there an easy way/smart way to transform my table into the new layout? Or programmatically in R? I need to do this in R, not before the Import.

预先感谢

推荐答案

您可以使用 Dummies 包轻松地创建虚拟变量。

You can easily make dummy variables by using the dummies package.

library(dummies)

df <- data.frame(
  time = c(20000616, 20000616, 20000616, 20000616, 20000616, 20000616), 
  hour = c(1, 2, 3, 4, 5, 6), 
  Money = c(9.35, 6.22, 10.65, 11.42, 10.12, 7.32), 
  day = c(5, 5, 5, 5, 5, 5))

# Specify the categorical variables in the dummy.data.frame function.
df_dummy <- dummy.data.frame(df, names=c("hour"), sep="_")
names(df_dummy) <- c("time", 1:6, "Money", "day")
df_dummy <- df_dummy[c("time", "Money", "day", 1:6)]
df_dummy
# time Money day 1 2 3 4 5 6
# 1 20000616  9.35   5 1 0 0 0 0 0
# 2 20000616  6.22   5 0 1 0 0 0 0
# 3 20000616 10.65   5 0 0 1 0 0 0
# 4 20000616 11.42   5 0 0 0 1 0 0
# 5 20000616 10.12   5 0 0 0 0 1 0
# 6 20000616  7.32   5 0 0 0 0 0 1

这篇关于列/变量的虚拟化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆