[英]Using apply to run a function on multiple columns in R
在將多個變量(列)按分類變量分組后,我正在嘗試收集它們的匯總統計信息。 我希望使用apply
function,而不是為每個變量單獨編寫代碼。
這是我想使用的 function 而不添加任何應用程序:
library(dplyr)
group_by(my_data, group) %>%
summarise(
count = n(),
mean = mean(weight, na.rm = TRUE),
sd = sd(weight, na.rm = TRUE)
)
我知道這是一個非常新手的問題,但我正在努力使用各種應用教程來解決這個問題。 我認為由於 pipe,我覺得它很棘手。
干杯!
底座 R:
data.frame(do.call("rbind", lapply(split(df, df$car_brand), function(x){
data.frame(
car_brand = x$car_brand,
counter = nrow(x),
avg = mean(x$hp, na.rm = T),
std_dev = ifelse(is.na(sd(x$hp, na.rm = T)), 0, sd(x$hp, na.rm = T)))
}
)
),
row.names = NULL
)
數據:
df <- data.frame(car_type = row.names(mtcars),
car_brand = gsub(" .*", "", row.names(mtcars)),
mtcars, row.names = NULL)
基於公式(例如lm
、 aov
等)的函數與group_by
存在問題,正如您在此處看到的那樣,因此,這是使用purrr::map_*
和group_split
的一種方法
library(dplyr)
library(purrr)
nms <- c('wt','qsec')
#loop over nms and apply the 2nd part, so .x will take 'wt' then `qsec`
map_dfc(nms,
~mtcars %>%
group_split(cyl) %>%
map_df(function(y) y %>%
summarise(!!quo_name(paste0("mean_",.x)):=mean(y[[.x]]),
!!quo_name(paste0("pval_",.x)):=summary(aov(as.formula(paste(.x,"~am")), data=y))[[1]]$`Pr(>F)`[1]
)
)
)
# A tibble: 3 x 4
mean_wt pval_wt mean_qsec pval_qsec
<dbl> <dbl> <dbl> <dbl>
1 2.29 0.0104 19.1 0.0166
2 3.12 0.00101 18.0 0.00514
3 4.00 0.218 16.8 0.000834
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.