r - 按列分组并查找另一列的先前值
问题描述
我有一个很长的销售数据,下面是一个示例性摘录:
| Date | CountryA | CountryB | PriceA | PriceB | |
+------------+----------+----------+--------+--------+--+
| 05/09/2019 | US | Japan | 20 | 55 | |
| 28/09/2019 | Japan | Germany | 30 | 28 | |
| 16/10/2019 | Canada | US | 25 | 78 | |
| 28/10/2019 | Germany | Japan | 60 | 17 | |
+------------+----------+----------+--------+--------+--+
我想对列“CountryB”进行分组,然后生成一个新列,该列显示该相应国家的 PriceA 的先前值,即当该特定国家最后一次基于日期顺序出现在“CountryA”列中时。在这个示例表中,我想得到以下结果:
| Date | CountryA | CountryB | PriceA | PriceB | PriceA_lag1 | |
+------------+----------+----------+--------+--------+-------------+--+
| 05/09/2019 | US | Japan | 20 | 55 | | |
| 28/09/2019 | Japan | Germany | 30 | 28 | | |
| 16/10/2019 | Canada | US | 25 | 78 | 20 | |
| 28/10/2019 | Germany | Japan | 60 | 17 | 30 | |
+------------+----------+----------+--------+--------+-------------+--+
我用 dplyr 尝试了以下操作:
data=data%>%group_by(CountryB)%>%mutate_at(list(lag1=~dplyr::lag(.,1,order_by=Date)),.vars=vars(PriceA))
但是,当相应国家/地区在“CountryA”列中时,这并没有给我前面的值,而是当相应国家/地区在“CountryB”中时。
有人可以帮我解决这个问题吗?谢谢。
解决方案
很可能是我写过的最丑陋的代码,但是......
# install.packages('dplyr', 'magrittr')
library(dplyr)
library(magrittr)
d <- data.frame(
stringsAsFactors = FALSE,
Date = c("05/09/2019", "28/09/2019", "16/10/2019", "28/10/2019"),
CountryA = c("US", "Japan", "Canada", "Germany"),
CountryB = c("Japan", "Germany", "US", "Japan"),
PriceA = c(20L, 30L, 25L, 60L),
PriceB = c(55L, 28L, 78L, 17L)
) %>%
mutate(Date = as.Date(Date, format = '%d/%m/%Y'))
priceA_lag <- c()
for(row in 1:nrow(d)){
country <- slice(d, row) %$% CountryB
date <- slice(d, row) %$% Date
thePrice <- d %>%
filter(CountryA == country,
date > Date) %>%
filter(Date == max(Date)) %$%
PriceA
thePrice <- ifelse(length(thePrice) > 0, thePrice, NA)
priceA_lag <- priceA_lag %>%
append(thePrice)
}
d$priceA_lag <- priceA_lag
> d
Date CountryA CountryB PriceA PriceB priceA_lag
1 2019-09-05 US Japan 20 55 NA
2 2019-09-28 Japan Germany 30 28 NA
3 2019-10-16 Canada US 25 78 20
4 2019-10-28 Germany Japan 60 17 30
推荐阅读
- asp.net-core - 我可以使用 Ocelot API Gateway 代理网站吗
- amazon-web-services - AWS - Mysfits,无法在 import-rest-api 期间创建授权人
- flutter - Flutter Firebase 提供程序架构
- android - 重置 onActivityResult 中的值
- css - css类中的垂直线
- python - 使用 Beautifulsoup 查找特定 ID 下的文章
- flutter - 为什么 Navigator.pop 无法传回参数?
- python - matplotlib 中 yticks/xticks 函数的刻度参数到底是什么?
- xml - 如何使用selenium java同时打开两个浏览器
- dictionary - Dart Map.removeWhere 不删除所有匹配删除条件的键