这是我在R中需要的一个例子,我有这样的数据
df <-
Scenario Group1 Group2
S1 8 9
S2 7 8
S3 6 7
S4 4 5
S5 3 4从上面的数据中,我想获得以下数据
df_converted <-
Scenario1 Scenario2 Group1 Group2
S1 S2 -1 -1
S1 S3 -2 -2
S1 S4 -4 -4
S1 S5 -5 -5
S2 S3 -1 -1
S2 S4 -3 -3
S2 S5 -4 -4
S3 S4 -2 -2
S3 S5 -3 -3
S4 S5 -1 -1新数据中的每一行都是通过减去前一行或滞后行来获得的。对于第一列中所有场景的组合,都可以得到这一减法。如果解决方案是在dplyr中,那么它将非常有用,谢谢。上面的例子是为了减法,我需要另一个转换后的数据,给出行间的百分比差异。
发布于 2022-03-02 11:45:39
您可以在这里使用full_join(),但是还有另一个选项(下面),它更快,并利用了combn()
full_join(df,df, by=character()) %>%
filter(Scenario.x<Scenario.y) %>%
mutate(
Group1 = Group1.y-Group1.x,
Group2 = Group2.y-Group2.x,
Perc1 = (Group1.y-Group1.x)/Group1.y,
Perc2 = (Group2.y-Group2.x)/Group2.y
) %>%
select(Scenario1 = Scenario.x,
Sceanrio2 = Scenario.y,
Group1, Group2, Perc1,Perc2)输出:
Scenario1 Sceanrio2 Group1 Group2 Perc1 Perc2
<chr> <chr> <dbl> <dbl> <dbl> <dbl>
1 S1 S2 -1 -1 -0.143 -0.125
2 S1 S3 -2 -2 -0.333 -0.286
3 S1 S4 -4 -4 -1 -0.8
4 S1 S5 -5 -5 -1.67 -1.25
5 S2 S3 -1 -1 -0.167 -0.143
6 S2 S4 -3 -3 -0.75 -0.6
7 S2 S5 -4 -4 -1.33 -1
8 S3 S4 -2 -2 -0.5 -0.4
9 S3 S5 -3 -3 -1 -0.75
10 S4 S5 -1 -1 -0.333 -0.25 更新后的、更快的选项:
# Make sure Scenario is sorted
df <- df %>% arrange(Scenario)
# Create a function the gets difference and percent, given all combn
f <- function(df) {
d= df[2,] - df[1,]
p = d/df[2,]
tibble(d,p)
}
# Column bind the results
do.call(cbind, list(
as_tibble(t(combn(df$Scenario,2)),.name_repair = ~c("Scenario1","Scenario2")),
df[,2:3] %>% map(~f(combn(.x,2)))
))输出:
Scenario1 Scenario2 Group1.d Group1.p Group2.d Group2.p
1 S1 S2 -1 -0.1428571 -1 -0.1250000
2 S1 S3 -2 -0.3333333 -2 -0.2857143
3 S1 S4 -4 -1.0000000 -4 -0.8000000
4 S1 S5 -5 -1.6666667 -5 -1.2500000
5 S2 S3 -1 -0.1666667 -1 -0.1428571
6 S2 S4 -3 -0.7500000 -3 -0.6000000
7 S2 S5 -4 -1.3333333 -4 -1.0000000
8 S3 S4 -2 -0.5000000 -2 -0.4000000
9 S3 S5 -3 -1.0000000 -3 -0.7500000
10 S4 S5 -1 -0.3333333 -1 -0.2500000https://stackoverflow.com/questions/71321829
复制相似问题