[英]Is there a way to make a 2 digit year into a 4 digit year in r
我開始學習r。 我們必須整理一個數據集。 日期列的日期為 May_08。 該列必須按月和年分開。 例如:從 May_08 到 2008 年 5 月。這是我迄今為止的代碼。
dataset %>%
separate(date, c("month","year"))
你可以使用strftime
。 只需在字符串前粘貼一天即可。
x <- "May_08"
strftime(as.Date(paste(1, x), format="%d %b_%y"), "%b %Y")
# [1] "May 2008"
您也可以使用lubridate 。
x <- "May_08"
library(lubridate)
paste(month(parse_date_time(x, "my"), label = T), year(parse_date_time(x, "my")), sep = " ")
# [1] "May 2008"
如果您知道系列中從 21 世紀年份打破 20 世紀的年份,則應該使用簡單的ifelse
語句。 在示例中,1990 年是突破年份,因此:
yrs <- c(91,94,97,00,03,06,09,12,15,18,21)
yrs <- ifelse(yrs>90, yrs+1900, yrs+2000)
> print(yrs)
[1] 1991 1994 1997 2000 2003 2006 2009 2012 2015 2018 2021
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.