I have dataframe of active users where it's 1 when the user was active this specific date and 0 when he was not.
I am trying to modify my dataframe from this:
df <- data.frame(userID=c("user123","user124","user125"),
`2021-03-01`=c(1,0,1),
`2021-03-08`=c(1,1,1),
`2021-03-15`=c(0,0,1))
to this dataframe in an elegant way:
df <- data.frame(userID=c("user123", "user125", "user123","user124","user125","user125"),
Active_WeekDate=c("2021-03-01","2021-03-01", "2021-03-08", "2021-03-08","2021-03-08","2021-03-15"))
I found this solution but is there a more elegant way?
library(tidyr)
df <- pivot_longer(df, cols = -userID, names_to="Active_WeekDate")
df <- df[ df$value==1, ]
df <- df[ , c(1,2)]
df
Any help much appreciated!