简体   繁体   English

从日期中提取月份并将其转换为单词中的月份

[英]Extract Month from a Date and convert it to Month in words

I have obtained the date like obtaining this "2018-05-31".我已经获得了像获得这个“2018-05-31”这样的日期。 Now, I want to create a new columnn which would be the month extracted, 05, and converting it to words "May".现在,我想创建一个新的列,它是提取的月份,05,并将其转换为单词“May”。 How could I do it?我怎么能做到?

df$Fecha<-as.Date(df$Fecha)
df$Fecha
[1] "2020-02-01" "2020-01-01" "2020-02-01" "2020-02-01"

df$Mes <- df$Fecha(Month = format(as.Date(df$Fecha), '%b'))
df$Mes

I get an error我收到一个错误

We can use format after converting to Date class.我们可以在转换为Date class 后使用format

df1 <- data.frame(Month = format(as.Date(str1), '%b'))
df1
#  Month
#1   May

If it is to create a new column in an already existing dataset如果是在已经存在的数据集中创建一个新列

df$Mes <- format(as.Date(df$Fecha), '%b')

If we need the full name, change the %b to %B如果我们需要全名,请将%b更改为%B

data数据

str1 <- "2018-05-31"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM