我有这个数据集
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是因变量
如何将日期转换成虚拟变量。 如果一天是指该日期,则为1,否则为0。
更清楚地说,正如我期望的那样
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
这不是重复的,因为我需要以0和1作为所需输出的数据帧结构。 “复制的”帖子对我没有帮助:(
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
我需要日期的名称(星期一,星期二...)
答案 0 :(得分:1)
也许是这样的:
library(dplyr)
library(lubridate)
library(tidyr)
df %>%
mutate(weekDay = lubridate::dmy(Data) %>% weekdays(),
value = 1) %>%
spread(key=weekDay, value=value, fill=0)