我想为一系列工作日分配一个周数(例如" mon"," tue"," wed" ..)。我的数据集是这样的:
2014
wed
thu
fri
sat
sun
mon
tue
我想创建一个这样的新列:
2014 Week 2014
wed 1
thu 1
fri 1
sat 1
sun 1
mon 2
tue 2
但是这个代码必须每年都有效,无论开始日期如何。 感谢
答案 0 :(得分:3)
x <- c("wed","thu", "fri", "sat", "sun", "mon", "tue")
cumsum(x == "mon") + (x[1] != "mon")
#[1] 1 1 1 1 1 2 2