如何在R编程中从50+级的巨大因子变量中创建因子?

时间:2019-06-27 10:28:21

标签: r

我的因子变量为31,计算一个月中的日期。我需要将此因子转换为3,即1-10作为“初月”,11-20作为“中月”,21-31作为“结束月”。

1 个答案:

答案 0 :(得分:1)

您可以按照Ronak Shah的建议使用cut函数

cut(1:31, breaks = c(0, 10, 20, 31), labels = c("early month", "mid month", "end month")

# [1] early month early month early month early month early month early month
# [7] early month early month early month early month mid month   mid month  
# [13] mid month   mid month   mid month   mid month   mid month   mid month  
# [19] mid month   mid month   end month   end month   end month   end month  
# [25] end month   end month   end month   end month   end month   end month  
# [31] end month  
# Levels: early month mid month end month