将日期转换为R中的伪变量 [英] transform date into dummy variable in R
本文介绍了将日期转换为R中的伪变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个数据集
df=structure(list(Data = structure(c(4L, 5L, 6L, 7L, 8L, 9L, 10L,
1L, 2L, 3L), .Label = c("01.01.2018", "02.01.2018", "03.01.2018",
"25.12.2017", "26.12.2017", "27.12.2017", "28.12.2017", "29.12.2017",
"30.12.2017", "31.12.2017"), class = "factor"), Y = 1:10), .Names = c("Data",
"Y"), class = "data.frame", row.names = c(NA, -10L))
日期是日期; Y是因变量
Date is date; Y-is dependent variable
如何将日期转换成虚拟变量. 如果一天是指该日期,则为1,否则为0.
How can i transform days of date into dummy variable. If the day refers to this date, then one, otherwise 0.
更明确地说,正如我期望的那样
To be more clear, as output i expect
Data Y Mon Tue Wed Thu Fri Sat Sun
25.12.2017 1 1 0 0 0 0 0 0
26.12.2017 2 0 1 0 0 0 0 0
27.12.2017 3 0 0 1 0 0 0 0
28.12.2017 4 0 0 0 1 0 0 0
29.12.2017 5 0 0 0 0 1 0 0
30.12.2017 6 0 0 0 0 0 1 0
31.12.2017 7 0 0 0 0 0 0 1
01.01.2018 8 1 0 0 0 0 0 0
02.01.2018 9 0 1 0 0 0 0 0
03.01.2018 10 0 0 1 0 0 0 0
编辑
在这里使用WEEKDAY()
功能在excel中显示天数
Edit
here screen with days in excel with WEEKDAY()
function
这不是重复的,因为我需要以0和1作为所需输出的数据帧结构. 复制的"帖子对我没有帮助:(
it is not dublicate, cause i need namely dataframe structure with 0 and 1 as desired output. "dublicated " posts doesn't help me :(
library(dummies)
df1 <- cbind(df, dummy(df$Data, sep = "_"))
输出
Data Y df_01.01.2018 df_02.01.2018 df_03.01.2018 df_25.12.2017 df_26.12.2017 df_27.12.2017 df_28.12.2017
1 25.12.2017 1 0 0 0 1 0 0 0
2 26.12.2017 2 0 0 0 0 1 0 0
3 27.12.2017 3 0 0 0 0 0 1 0
4 28.12.2017 4 0 0 0 0 0 0 1
5 29.12.2017 5 0 0 0 0 0 0 0
6 30.12.2017 6 0 0 0 0 0 0 0
7 31.12.2017 7 0 0 0 0 0 0 0
8 01.01.2018 8 1 0 0 0 0 0 0
9 02.01.2018 9 0 1 0 0 0 0 0
10 03.01.2018 10 0 0 1 0 0 0 0
df_29.12.2017 df_30.12.2017 df_31.12.2017
1 0 0 0
2 0 0 0
3 0 0 0
4 0 0 0
5 1 0 0
6 0 1 0
7 0 0 1
8 0 0 0
9 0 0 0
10 0 0 0
我需要日期的名称(星期一,星期二...)
I need name of days (mon,tue...)
推荐答案
也许是这样的:
library(dplyr)
library(lubridate)
library(tidyr)
df %>%
mutate(weekDay = lubridate::dmy(Data) %>% weekdays(),
value = 1) %>%
spread(key=weekDay, value=value, fill=0)
这篇关于将日期转换为R中的伪变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文