dplyr::c_across
- 여러 열을 합쳐주는 역할을 한다
- 예제 데이터
df <- tibble(id = 1:4, w = runif(4), x = runif(4), y = runif(4), z = runif(4))
df %>%
rowwise() %>%
mutate(
sum = sum(c_across(w:z)),
sd = sd(c_across(w:z))
)
#> # A tibble: 4 × 7
#> # Rowwise:
#> id w x y z sum sd
#> <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 1 0.790 0.588 0.142 0.225 1.74 0.305
#> 2 2 0.892 0.514 0.781 0.207 2.39 0.305
#> 3 3 0.327 0.317 0.456 0.659 1.76 0.159
#> 4 4 0.351 0.408 0.234 0.715 1.71 0.205
목표
- NA값을 제외하고 각 HV5와 HV5를 제외한 값의 평균을 구해보자
데이터
data <- tribble(
~Site, ~HV1, ~HV2, ~HV3, ~HV4, ~HV5, ~HV6, ~HV7, ~HV8, ~HV9, ~HV10,
"A", 0.142, 0.0787, NA, 0.338, 0.797, 0.231, NA, 0.461, 0.442, 0.0579,
"B", 0.243, 0.0725, 0.354, 0.368, 1.02, 0.105, NA, 0.555, 0.0644, 0,
"C", 0, NaN, 0, 0.605, 0, 0.417, 0.589, 0, NaN, 0,
"D", NaN, 0, 0, 0.111, 0, 0, 0.0412, 0, NaN, 0,
"E", 0, 0, 0, 0.188, 0.749, 0.0113, 0.117, 0.499, 0, 0
)
data
계산
subject <-c("HV1", "HV2", "HV3", "HV4", "HV6", "HV7", "HV8", "HV9", "HV10")
result <- data %>%
dplyr::group_by(Site) %>%
dplyr::summarise(
mean_HV5 = mean(HV5, na.rm = TRUE),
mean_Other = mean(c_across(all_of(subject)), na.rm = TRUE)
)
result
# # A tibble: 5 × 3
# Site mean_HV5 mean_Other
# <chr> <dbl> <dbl>
# 1 A 0.797 0.250
# 2 B 1.02 0.220
# 3 C 0 0.230
# 4 D 0 0.0217
# 5 E 0.749 0.0906
출처
반응형