将月份名称转换为 r 中的数字
Convert month names to numbers in r
我正在使用包含完整月份名称的数据:
months <- c("March", "April", "May", "June", "July", "August", "September")
是否有将它们转换为数字的函数?
非常感谢
您可以将 match()
与内置变量 month.name
一起使用。
match(months, month.name)
[1] 3 4 5 6 7 8 9
或者将您的月份变量转换为一个因子,然后是一个整数:
as.integer(factor(months, levels = month.name))
另一种方法是 link 通过命名向量
月份数字和名称
months <- c("March", "April", "May", "June", "July", "August", "September")
x <- setNames(1:12, month.name)
unname(x[months] )
我正在使用包含完整月份名称的数据:
months <- c("March", "April", "May", "June", "July", "August", "September")
是否有将它们转换为数字的函数?
非常感谢
您可以将 match()
与内置变量 month.name
一起使用。
match(months, month.name)
[1] 3 4 5 6 7 8 9
或者将您的月份变量转换为一个因子,然后是一个整数:
as.integer(factor(months, levels = month.name))
另一种方法是 link 通过命名向量
月份数字和名称months <- c("March", "April", "May", "June", "July", "August", "September")
x <- setNames(1:12, month.name)
unname(x[months] )