If your schedule is stored as a matrix or data frame, you can use the reshape2
package:
# generate a fake schedule
sched <- matrix(rbinom(25, 1, 1/2), 5,
dimnames = list(1:5, c("Mo", "Tu", "We", "Th", "Fr")))
library(reshape2)
melt(sched)
# Var1 Var2 value
# 1 1 Mo 0
# 2 2 Mo 0
# 3 3 Mo 1
# 4 4 Mo 0
# 5 5 Mo 1
# 6 1 Tu 0
# 7 2 Tu 1
# 8 3 Tu 1
# 9 4 Tu 0
# 10 5 Tu 1
# 11 1 We 1
# 12 2 We 1
# 13 3 We 1
# 14 4 We 0
# 15 5 We 0
# 16 1 Th 0
# 17 2 Th 1
# 18 3 Th 1
# 19 4 Th 0
# 20 5 Th 0
# 21 1 Fr 0
# 22 2 Fr 1
# 23 3 Fr 1
# 24 4 Fr 1
# 25 5 Fr 1
There is also the function gather
in the package tidyr
but it only works on data frames and it’s a bit more advanced overall.
To get only the rows with 1
in the value
column, do:
sched <- melt(sched)
sched[sched$value == 1, ]
2
solved Convert a schedule (in wide form) to long form in R and select only some rows [closed]