将月份字符值转换为日期月份值

时间:2018-12-02 19:48:56

标签: r zoo lubridate

我有一个带有月份名称字符列的数据框,我需要将其转换为月份名称日期列。我尝试这段代码

Enum litteral value: '1' - memory representation: -1
No previous
Next: '2'

Enum litteral value: '2' - memory representation:  2
Previous: '1'
Next: '3'

Enum litteral value: '3' - memory representation:  3
Previous: '2'
Next: 'J'

Enum litteral value: 'J' - memory representation:  11
Previous: '3'
Next: Q

Enum litteral value: Q - memory representation:  12
Previous: J
Next: K

Enum litteral value: K - memory representation:  13
Previous: Q
Next: ACE

Enum litteral value: ACE - memory representation:  14
Previous: K
No next

Handling of user input: Unknown user value
Exception catched: raised CONSTRAINT_ERROR : bad input for 'Value: "Unknown user value"

Setting value to Ace instead
tmp2 value: ACE - memory representation:  14

Memory mapping would result in invalid enum for value: -3
Memory mapping would result in invalid enum for value: -2
Enum with value: -1 is valid.
Memory mapping would result in invalid enum for value:  0
Memory mapping would result in invalid enum for value:  1
Enum with value:  2 is valid.
Enum with value:  3 is valid.
Memory mapping would result in invalid enum for value:  4
Memory mapping would result in invalid enum for value:  5
Memory mapping would result in invalid enum for value:  6
Memory mapping would result in invalid enum for value:  7
Memory mapping would result in invalid enum for value:  8
Memory mapping would result in invalid enum for value:  9
Memory mapping would result in invalid enum for value:  10
Enum with value:  11 is valid.
Enum with value:  12 is valid.
Enum with value:  13 is valid.
Enum with value:  14 is valid.
Memory mapping would result in invalid enum for value:  15

,但所有值都变为NA。我在做什么错了?

谢谢。

有关数据的更多信息

data1$month <- as.Date(as.character(data1$month), "%B")

1 个答案:

答案 0 :(得分:1)

您可以使用预定义的向量match month.abb月份名称的前三个字符:

data1$month_num <- match(substr(data1$month, 1, 3), month.abb)

希望有帮助。