问题
I have this dataset
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))
Date is date; Y-is dependent variable
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
Edit
here screen with days in excel with WEEKDAY()
function
Edit2
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 = "_"))
output
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...)
回答1:
Maybe something like this:
library(dplyr)
library(lubridate)
library(tidyr)
df %>%
mutate(weekDay = lubridate::dmy(Data) %>% weekdays(),
value = 1) %>%
spread(key=weekDay, value=value, fill=0)
来源:https://stackoverflow.com/questions/51231385/transform-date-into-dummy-variable-in-r