根据条件按列元素进行变异
Mutate by a column element based on a condition
我正在尝试改变一个列:
- 数据帧根据错误分组
- 突变列 (treatmentToGrowthRatio) 等于:
价值/(治疗==“增长”的价值)。
我有:
df <- tibble(
type = as.factor(c("bug1", "bug1", "bug1", "bug2", "bug2", "bug2", "bug3", "bug3", "bug3", "blank")),
treatment = c(rep(c("TreatA", "TreatB", "Growth"),3), "Blank"),
value = 1:10
)
我有效执行的操作:
df %>% group_by(bug) %>%
mutate(
treatmentToGrowthRatio =
value/
## value where treatment == growth
## (i.e. for bug 1 = 3; for bug 2 = 6; for bug 3 = 9; for Blank = NA)
)
给出想要的输出:
dfFinal <- tibble(
type = as.factor(c("bug1", "bug1", "bug1", "bug2", "bug2", "bug2", "bug3", "bug3", "bug3", "blank")),
treatment = c(rep(c("TreatA", "TreatB", "Growth"),3), "Blank"),
value = 1:10,
treatmentToGrowthRatio = c(1/3, 2/3, 1, 4/6, 5/6, 1, 7/9, 8/9, 1, NA)
)
我得到的最接近的是 treatmentToGrowthRatio = 1 其中 Treatment == "Growth" 来自:
df %>% group_by(type) %>%
mutate(
treatmentToGrowthRatio =
value/
case_when(
str_detect(treatment,
"Growth") ~ value
)
)
感谢任何见解!谢谢
你的问题表述不清楚。 type == "blank"
组没有 treatment == "Growth"
。在这种情况下,您预计会发生什么情况?
注意到我上面的评论,我将忽略带有 type == "blank"
的行。然后你要么做
library(dplyr)
df %>%
filter(type != "blank") %>%
group_by(type) %>%
mutate(treatmentToGrowthRatio = value / value[treatment == "Growth"]) %>%
ungroup()
## A tibble: 9 x 4
# type treatment value treatmentToGrowthRatio
# <fct> <chr> <int> <dbl>
#1 bug1 TreatA 1 0.333
#2 bug1 TreatB 2 0.667
#3 bug1 Growth 3 1
#4 bug2 TreatA 4 0.667
#5 bug2 TreatB 5 0.833
#6 bug2 Growth 6 1
#7 bug3 TreatA 7 0.778
#8 bug3 TreatB 8 0.889
#9 bug3 Growth 9 1
或者(也许更优雅)从长到宽整形,然后从相关列中划分值。
library(dplyr)
library(tidyr)
df %>%
pivot_wider(names_from = treatment) %>%
mutate(across(starts_with("Treat"), ~ .x / Growth))
## A tibble: 4 x 5
# type TreatA TreatB Growth Blank
# <fct> <dbl> <dbl> <int> <int>
#1 bug1 0.333 0.667 3 NA
#2 bug2 0.667 0.833 6 NA
#3 bug3 0.778 0.889 9 NA
#4 blank NA NA NA 10
然后根据需要再次整形。
我正在尝试改变一个列:
- 数据帧根据错误分组
- 突变列 (treatmentToGrowthRatio) 等于:
价值/(治疗==“增长”的价值)。
我有:
df <- tibble(
type = as.factor(c("bug1", "bug1", "bug1", "bug2", "bug2", "bug2", "bug3", "bug3", "bug3", "blank")),
treatment = c(rep(c("TreatA", "TreatB", "Growth"),3), "Blank"),
value = 1:10
)
我有效执行的操作:
df %>% group_by(bug) %>%
mutate(
treatmentToGrowthRatio =
value/
## value where treatment == growth
## (i.e. for bug 1 = 3; for bug 2 = 6; for bug 3 = 9; for Blank = NA)
)
给出想要的输出:
dfFinal <- tibble(
type = as.factor(c("bug1", "bug1", "bug1", "bug2", "bug2", "bug2", "bug3", "bug3", "bug3", "blank")),
treatment = c(rep(c("TreatA", "TreatB", "Growth"),3), "Blank"),
value = 1:10,
treatmentToGrowthRatio = c(1/3, 2/3, 1, 4/6, 5/6, 1, 7/9, 8/9, 1, NA)
)
我得到的最接近的是 treatmentToGrowthRatio = 1 其中 Treatment == "Growth" 来自:
df %>% group_by(type) %>%
mutate(
treatmentToGrowthRatio =
value/
case_when(
str_detect(treatment,
"Growth") ~ value
)
)
感谢任何见解!谢谢
你的问题表述不清楚。 type == "blank"
组没有 treatment == "Growth"
。在这种情况下,您预计会发生什么情况?
注意到我上面的评论,我将忽略带有 type == "blank"
的行。然后你要么做
library(dplyr)
df %>%
filter(type != "blank") %>%
group_by(type) %>%
mutate(treatmentToGrowthRatio = value / value[treatment == "Growth"]) %>%
ungroup()
## A tibble: 9 x 4
# type treatment value treatmentToGrowthRatio
# <fct> <chr> <int> <dbl>
#1 bug1 TreatA 1 0.333
#2 bug1 TreatB 2 0.667
#3 bug1 Growth 3 1
#4 bug2 TreatA 4 0.667
#5 bug2 TreatB 5 0.833
#6 bug2 Growth 6 1
#7 bug3 TreatA 7 0.778
#8 bug3 TreatB 8 0.889
#9 bug3 Growth 9 1
或者(也许更优雅)从长到宽整形,然后从相关列中划分值。
library(dplyr)
library(tidyr)
df %>%
pivot_wider(names_from = treatment) %>%
mutate(across(starts_with("Treat"), ~ .x / Growth))
## A tibble: 4 x 5
# type TreatA TreatB Growth Blank
# <fct> <dbl> <dbl> <int> <int>
#1 bug1 0.333 0.667 3 NA
#2 bug2 0.667 0.833 6 NA
#3 bug3 0.778 0.889 9 NA
#4 blank NA NA NA 10
然后根据需要再次整形。