Диаграмма, показывающая, чего я пытаюсь достичь:
Здравствуйте, сообщество. Я пытаюсь изменить новую переменную в зависимости от средней функции одной указанной c группы (отфильтрованной и сгруппированной). Когда я пытаюсь создать новую переменную, я разгруппирую набор для работы во всех группах. Я попытался запустить этот код R . Однако функция mutate применяется только к отфильтрованной группе и не может найти функцию без фильтра. Я dput () образец моего фрейма данных ниже (df01) . Заранее большое спасибо за все ваши комментарии и предложения. С уважением. M.
R код :
df01 %>% #summary table of the means to be used.
filter(GFPimg == "WT") %>%
group_by(Demineralization, Cond, Temp) %>%
summarise(Mean2 = mean(Mean)) %>%
arrange(desc(Demineralization))
print()
df01 %>%
filter(GFPimg == "WT") %>%
group_by(Demineralization, Cond, Temp) %>%
mutate(mean2 = mean(Mean)) %>%
arrange(desc(Demineralization, Cond)) %>%
ungroup() %>%
group_by(Demineralization, Cond) %>%
mutate(submean = Mean - mean2) %>%
print(n=200)
Пример кадра данных df01 :
df01 <- structure(list(ID = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54),
GFPimg = structure(c(1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 3L, 3L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, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L), .Label = c("37c", "RT"), class = "factor"),
Side = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("L", "R"), class = "factor"),
Mean = c(62.435, 64.537, 102.447, 92.608, 103.277, 104.711, 67.017, 61.748, 68.921, 59.962, 63.368, 60.435, 69.54, 67.886, 51.71, 50.291, 50.881, 54.865, 80.538, 84.05, 92.223, 87.337, 90.444, 90.728, 29.951, 28.574, 30.896, 30.399, 29.773, 30.715, 31.498, 30.385, 99.004, 83.644, 95.962, 83.451, 22.649, 22.5, 53.066, 51.368, 55.459, 57.203, 54.444, 58.504, 76.518, 95.81, 23.43, 24.736, 28.86, 28.347, 28.386, 29.319, 58.017, 63.064, 80.293, 89.194, 70.52, 63.989, 71.436, 59.379, 75.986, 80.22, 71.583, 76.589, 77.138, 95.998, 77.193, 71.384, 75.614, 83.061, 73.062, 71.833, 71.83, 55.783, 77.376, 64, 96.14, 99.876, 40.972, 53.465, 36.25, 47.626, 40.619, 39.697, 34.34, 36.855, 77.131, 75.35, 67.014, 58.811, 39.237, 49.357, 74.333, 79.921, 62.631, 63.119, 60.207, 65.171, 77.563, 82.078, 39.115, 45.988, 42.65, 55.806, 33.534, 41.271, 62.359, 67.092),
Demineralization = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("After", "Before"), class = "factor")), class = "data.frame", row.names = c(NA, -108L))