如何减去数据集中某些行和每个面板中的值

问题描述 投票:0回答:1

数据为:

   df <- structure(list(country = c("Poland", "Poland", "Poland", "Poland", "Poland", "Poland","Portugal", "Portugal", "Portugal", "Portugal", "Portugal", "Portugal", "Spain", "Spain", "Spain", "Spain", "Spain", "Spain"), Code = c("POL", "POL", "POL", "POL", "POL", "POL", "PRT", "PRT", "PRT", "PRT", "PRT", "PRT", "ESP", "ESP", "ESP", "ESP", "ESP", "ESP"), year = c(1950, 1951, 1952, 1953, 1954,1955, 1950, 1951, 1952, 1953, 1954, 1955, 1950, 1951, 1952, 1953, 1954, 1955), IV = c(1, 1, 1, 2, 3, 3, 1, 1, 1, 2, 2, 1, 1, 1, 2, 3, 4, 5)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -18L))

我想从 1952 年减去 1955 年的 IV 值; 1954-1951; 1953-1950。对于每个国家来说也是如此。在结果数据集中(我们将其命名为“newdata”),新变量“newIV”必须显示这种差异; 1950、1951 和 1952 年的值应删除,如下所示:

enter image description here

r dataframe
1个回答
0
投票

您可以

merge
aggregate
d
subset
获得临时
iv55
列,您可以将其减去,最后
subset
加上所需的年份。

> merge(df, 
+       aggregate(cbind(iv55=IV) ~ Code, subset(df, year == 1955), I), all=TRUE,
+       sort=FALSE
+ ) |> transform(IV=IV - iv55, iv55=NULL) |> subset(!year %in% 1950:1952)
   Code  country year IV
4   POL   Poland 1953 -1
5   POL   Poland 1954  0
6   POL   Poland 1955  0
10  PRT Portugal 1953  1
11  PRT Portugal 1954  1
12  PRT Portugal 1955  0
16  ESP    Spain 1953 -2
17  ESP    Spain 1954 -1
18  ESP    Spain 1955  0
© www.soinside.com 2019 - 2024. All rights reserved.