我在简单的情况下使用了cut()函数,但这是我第一次需要将数字[1到12]转换为标签作为数据框的附加列中的因子。
labs <- c("Jan", "Feb", "Mar", "Apr", "May", "June", "Jul","Aug", "Sept", "Oct","Nov","Dec")
length(labs) #12
sequence = c(1,2,3,4,5,6,7,8,9,10,11,12)
length(sequence) #12
months_df$Month <- cut(months_df$NumericMonth,breaks = sequence,labels = labs)
选择NumericMonth和CompleteDate的列
Ind NumericMonth CompleteDate
9 2 1999-02-19
15 1 1999-01-08
38 6 1998-06-12
61 1 1999-01-08
76 5 1998-05-01
115 5 1998-05-15
124 7 1998-07-11
错误:
cut.default出错(months_df $ NumericMonth,breaks = sequence,labels = labs):
“休息”和“标签”的长度不同
我想要的输出
一个新列,其中包含基于NumericMonth
的因子(带标签)如何将数字月转换为带标签的因子?