我有以下数据集:
usd year
1 65.09 1997
2 69.28 1998
3 71.18 1999Q1
4 72.12 1999Q2
5 70.68 1999Q3
6 71.01 1999Q4
7 71.45 2000Q1
8 72.02 2000Q2
9 72.29 2000Q3
10 71.12 2000Q4
我想拥有每年的手段:
usd year
1 65.09 1997
2 69.28 1998
3 71.24 1999
7 71.72 2000
如果我只有几年没有这个季度,我知道如何做到这一点。有没有办法提取岁月?也许用grep?
答案 0 :(得分:0)
我找到了使用 stringr
包的解决方案:
mydata <- data.frame(usd = c(65.09,69.28,71.18,72.12,70.68,71.01,71.45,72.02,72.29,71.12),
year = c("1997","1998","1999Q1","1999Q2","1999Q3","1999Q4",
"2000Q1","2000Q2","2000Q3","2000Q4"))
library(stringr)
mydata$year <- str_extract(mydata$year, "[[:digit:]]{4}")
mydata <- aggregate(usd ~ year, mydata, mean)
mydata
year usd
1 1997 65.0900
2 1998 69.2800
3 1999 71.2475
4 2000 71.7200