我有以下dataframe
:
structure(list(trial = c("ES1-7", "ES1-7", "ES1-7", "ES14-25",
"ES14-25", "ES14-25", "ES26-38", "ES26-38", "ES26-38", "ES8-13",
"ES8-13", "ES8-13", "SA1-13", "SA1-13", "SA1-13", "SA14-25",
"SA14-25", "SA14-25"), marker = c("0", "1", "2", "0", "1", "2",
"0", "1", "2", "0", "1", "2", "0", "1", "2", "0", "1", "2"),
n = c(873L, 269L, 2114L, 2300L, 673L, 5959L, 3410L, 1200L,
7930L, 599L, 173L, 1538L, 2834L, 509L, 6953L, 3082L, 757L,
6875L), prop = c(0.268120393120393, 0.0826167076167076, 0.649262899262899,
0.257501119570085, 0.0753470667263771, 0.667151813703538,
0.271929824561404, 0.0956937799043062, 0.63237639553429,
0.259307359307359, 0.0748917748917749, 0.665800865800866,
0.275252525252525, 0.0494366744366744, 0.6753108003108, 0.287661004293448,
0.0706552174724659, 0.641683778234086)), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -18L), vars = "trial",
labels = structure(list(
trial = c("ES1-7", "ES14-25", "ES26-38", "ES8-13", "SA1-13",
"SA14-25")), row.names = c(NA, -6L), class = "data.frame", vars = "trial",
drop = TRUE), indices = list(
0:2, 3:5, 6:8, 9:11, 12:14, 15:17), drop = TRUE, group_sizes = c(3L,
3L, 3L, 3L, 3L, 3L), biggest_group_size = 3L)
看起来像这样:
# A tibble: 6 x 4
# Groups: trial [2]
trial marker n prop
<chr> <chr> <int> <dbl>
1 ES1-7 0 873 0.268
2 ES1-7 1 269 0.0826
3 ES1-7 2 2114 0.649
4 ES14-25 0 2300 0.258
5 ES14-25 1 673 0.0753
6 ES14-25 2 5959 0.667
我想每group_by
trial
,并在prop
等于marker
时将1
值的一半添加到其他行中等于marker
或0
。
例如,当按2
分组时,当ES1-7
等于prop
时,我将得到以下marker
值:0.268+(0.0826 / 2)。
ES1-7和ES14-25组的预期输出为:
0
答案 0 :(得分:1)
还有dplyr
的可能性:
df %>%
group_by(trial) %>%
mutate(prop = ifelse(marker != 1, prop + prop[marker == 1]/2, prop))
trial marker n prop
<chr> <chr> <int> <dbl>
1 ES1-7 0 873 0.309
2 ES1-7 1 269 0.0826
3 ES1-7 2 2114 0.691
4 ES14-25 0 2300 0.295
5 ES14-25 1 673 0.0753
6 ES14-25 2 5959 0.705
7 ES26-38 0 3410 0.320
8 ES26-38 1 1200 0.0957
9 ES26-38 2 7930 0.680
10 ES8-13 0 599 0.297
答案 1 :(得分:0)
分两个步骤进行。 首先通过试验进行分组,并在标记== 1上进行过滤,然后计算得出1/2道具。 然后将其与原始data.frame结合在一起,并以试用为键, 计算出的prop.new = prop + prop.half。 可以将其简化为一个步骤的过程,但是认为这是您尝试更多代码的良好起点
esl当然是您的data.frame
esl %>% group_by(trial) %>%
filter(marker ==1) %>%
mutate(prop.half = prop/2) %>% select(trial,marker,prop.half) -> esl.half
left_join(esl, esl.half, by="trial") %>%
mutate(prop.new = prop+prop.half)