英文:
Replace string pattern in Column Date with the correct format in R
问题
在我的数据框中,日期列的格式不标准。我想将所有"x1Q2013"更改为"2013-01-01","x2Q2013"更改为"2013-04-01",以此类推,直到"X1Q2023"更改为"2023-01-01"。
谢谢!
gsub可以删除前面的部分,但如何添加后面的部分并保留年份呢?
英文:
Under my data frame, the Date column only is not in the standard format. I want to change anything that say x1Q2013 to 2013-01-01 x2Q2013 to 2013-04-01 so forth until X1Q2023 to 2023-01-01
Thank you!
gsub can remove the front part, but how to add the back part and keep the year.
答案1
得分: 1
使用 zoo
可以将季度年份转换为日期:
library(zoo)
dates = c("x1Q2013", "x2Q2013")
as.Date(as.yearqtr(dates, format = "x%qQ%Y"))
#[1] "2013-01-01" "2013-04-01"
英文:
With zoo
you can convert quarter year to dates:
library(zoo)
dates = c("x1Q2013", "x2Q2013")
as.Date(as.yearqtr(dates, format = "x%qQ%Y"))
#[1] "2013-01-01" "2013-04-01"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论