【发布时间】:2022-08-13 21:44:02
【问题描述】:
标签: r
标签: r
无论哪种情况,这都不是真正的“约会”。 zoo 包确实定义了一个 yearmon 类。在这里,我们可以只使用 strsplit 并处理月份字符,匹配 R 常量 month.abb,然后重新加入:
dat <- scan(text="Mar-92,Feb-93,Jul-94,Sep-95", what = "", sep=",")
#Read 4 items
datspl <- strsplit(dat, split="-")
sapply( datspl, function(mnyr){ paste( match(mnyr[1], month.abb), mnyr[2], sep="/")})
#[1] "3/92" "2/93" "7/94" "9/95"
【讨论】:
我们也可以使用stringr的str_replace_all:
data <- c("Mar-92", "Feb-93", "Jul-94")
str_replace_all(data, setNames(as.character(1:12), month.abb))
输出:
[1] "3-92" "2-93" "7-94"
【讨论】: