Reputation: 93
I have a dataframe that represents the begnning (dtime_b) and end (dtime) of a fiscal quarter, as well a list of company members (m1-m3) and when they first joined the company. Note, the date format is "YYYY-MM-DD". The dataframe looks like this:
df <- data.frame(dtime = c("2014-12-31", "2015-03-31", "2015-06-30", "2015-09-30"),
dtime_b = c("2014-09-30", "2014-12-31", "2015-03-31", "2015-06-30"),
m1 = c("2014-10-04", "2014-10-04", "2020-02-19", "2020-02-19"),
m2 = c("2015-1-14", "2017-11-14", "2017-11-14", "2017-11-14"),
m3 = c("2013-11-14", "2013-11-14", "2013-11-14", "2013-11-14")
)
If there is a change in the m1-m3 columns, this means that one member has been replaced. My goal is to create a total of three dummies where each dummie is a one, if the date the member joined the company falls between the dtime_b and dtime of that row.
For this, I have written the following code:
df$dummy_m1 <- ifelse(df$m1 <= df$dtime & df$m1 > df$dtime_b, 1, 0)
df$dummy_m1[is.na(df$dummy_m1)] <- 0
df$dummy_m2 <- ifelse(df$m2 <= df$dtime & df$m2 > df$dtime_b, 1, 0)
df$dummy_m2[is.na(df$dummy_m2)] <- 0
df$dummy_m3 <- ifelse(df$m3 <= df$dtime & df$m3 > df$dtime_b, 1, 0)
df$dummy_m3[is.na(df$dummy_m3)] <- 0
While this code works, I have a lot of these members and writing them all out would leave me with a lot of possibilites for mistakes. I'd appreciate your help in cutting down this code!
Upvotes: 0
Views: 28
Reputation: 51974
Use across
from dplyr
:
library(dplyr)
df %>%
mutate(across(m1:m3, ~ ifelse(.x <= dtime & .x > dtime_b, 1, 0),
.names = "dummy_{col}"))
output
dtime dtime_b m1 m2 m3 dummy_m1 dummy_m2 dummy_m3
1 2014-12-31 2014-09-30 2014-10-04 2015-1-14 2013-11-14 1 0 0
2 2015-03-31 2014-12-31 2014-10-04 2017-11-14 2013-11-14 0 0 0
3 2015-06-30 2015-03-31 2020-02-19 2017-11-14 2013-11-14 0 0 0
4 2015-09-30 2015-06-30 2020-02-19 2017-11-14 2013-11-14 0 0 0
Upvotes: 1