有没有办法从日期中提取月份和日期,如下所示
> asd <- data.frame(a = c('2020-02-15', '2020-03-16'))
> asd
a
1 2020-02-15
2 2020-03-16
预期产出
> asd
a b
1 2020-02-15 Feb-15
2 2020-03-16 Mar-16
回答1
转换为 Date
类和 format
它
asd <- transform(asd, b = format(as.Date(a), '%b-%d'))
asd
a b
1 2020-02-15 Feb-15
2 2020-03-16 Mar-16
回答2
这是使用 strftime
的另一个选项:
asd$b <- strftime(asd$a, '%b-%d')
输出
a b
1 2020-02-15 Feb-15
2 2020-03-16 Mar-16
或者我们可以使用 dplyr
来做同样的事情:
library(dplyr)
asd %>%
mutate(b = strftime(a, '%b-%d'))