Это ваши данные.
db <- structure(list(Y1 = c(1, 0, 1, 0, 1, 0, 1, 0, 1, 0), Y2 = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0), Y3 = c(1, 0, 1, 0, 1, 0, 1, 0, 1,
0), Sex = c("Male", "Female", "Male", "Female", "Male", "Female",
"Male", "Female", "Male", "Female"), X1 = c(52, 42, 46, 60, 81,
44, 61, 46, 56, 54), X2 = c(2, 1, 4, 3, 1, 2, 4, 1, 1, 2), X3 = c(7.25,
2, 0.08, 5.65, 1.37, 0.87, 0.87, 2, 7.25, 2), X4 = c(11.4, 27,
16.2, 2, 9.2, 15.4, 19.4, 7.2, 1.4, 25.2), X5 = c(0.5, 1, 0.17,
1.68, 0.8, 1, 0.25, 1, 0.45, 1), X6 = c(2, 2, 3, 1, 0, 0, 2,
1, 2, 3)), row.names = c(NA, -10L), class = c("tbl_df", "tbl",
"data.frame"))
Это код.Я думал, что Y1, Y2 и Y3 должны быть изменены с широкого на длинный формат.Вот почему я сначала использую функцию сбора.
db_pro1 <- db %>%
gather(y, value, starts_with("Y")) %>%
mutate(y_value = paste0(y, "-" ,value)) %>%
group_by(y_value, Sex) %>%
summarise_at(vars(starts_with("X")), funs(mean = mean(.), sd = sd(.)))
# A tibble: 6 x 14
# Groups: y_value [5]
y_value Sex X1_mean X2_mean X3_mean X4_mean X5_mean X6_mean X1_sd X2_sd X3_sd
<chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 Y1-0 Fema~ 49.2 1.8 2.50 15.4 1.14 1.4 7.56 0.837 1.83
2 Y1-1 Male 59.2 2.4 3.36 11.5 0.434 1.8 13.4 1.52 3.58
3 Y2-0 Fema~ 49.2 1.8 2.50 15.4 1.14 1.4 7.56 0.837 1.83
4 Y2-0 Male 59.2 2.4 3.36 11.5 0.434 1.8 13.4 1.52 3.58
5 Y3-0 Fema~ 49.2 1.8 2.50 15.4 1.14 1.4 7.56 0.837 1.83
6 Y3-1 Male 59.2 2.4 3.36 11.5 0.434 1.8 13.4 1.52 3.58
# ... with 3 more variables: X4_sd <dbl>, X5_sd <dbl>, X6_sd <dbl>