[英]Subtracting factor level from another
Given the following data frame:给定以下数据框:
set.seed(42)
test <- data.frame(tb = c(rep("top", 5), rep("bottom", 5)),
name = rep(c("q1", "q2", "q3", "q4", "q5"), 2),
values = rnorm(10, 1))
I'd like to subtract each "name" (bottom) level from its corresponding (top) value.我想从相应的(顶部)值中减去每个“名称”(底部)级别。 For example: top q1 - bottom q1 = 1.4770829, top q2 - bottom q2 = -2.0762202, etc.
例如:顶部 q1 - 底部 q1 = 1.4770829,顶部 q2 - 底部 q2 = -2.0762202 等。
How can I produce the following results?我怎样才能产生以下结果? In reality, "name" will have many more levels than five.
实际上,“名称”的级别将远远超过五级。
q1 1.4770829
q2 -2.0762202
q3 0.4577874
q4 -1.3855611
q5 0.4669824
You can reshape the data and subtract the two columns.您可以重塑数据并减去两列。
library(dplyr)
library(tidyr)
test %>%
pivot_wider(names_from = tb, values_from = values) %>%
mutate(diff = top - bottom)
# name top bottom diff
# <chr> <dbl> <dbl> <dbl>
#1 q1 2.37 0.894 1.48
#2 q2 0.435 2.51 -2.08
#3 q3 1.36 0.905 0.458
#4 q4 1.63 3.02 -1.39
#5 q5 1.40 0.937 0.467
Another way would be -另一种方法是 -
test %>%
arrange(name, tb) %>%
group_by(name) %>%
summarise(values = diff(values))
# name values
# <chr> <dbl>
#1 q1 1.48
#2 q2 -2.08
#3 q3 0.458
#4 q4 -1.39
#5 q5 0.467
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.