gpt4 book ai didi

r - 如何创建一个函数来获取汇总统计数据作为列?

转载 作者:行者123 更新时间:2023-12-02 16:01:20 25 4
gpt4 key购买 nike

我有三个工作流程来获取平均值、标准差和方差。是否可以通过使用一个表创建一个函数并将所有摘要作为结果来简化这一过程?

平均值

iris %>% 
select(-Species) %>%
summarise_all( , mean, na.rm = TRUE) %>%
t() %>%
as.data.frame() %>%
rownames_to_column("Name") %>%
rename(Mean = V1)

标准偏差

iris %>% 
select(-Species) %>%
summarise_all(., sd, na.rm = TRUE) %>%
t() %>%
as.data.frame() %>%
rownames_to_column("Name") %>%
rename(SD = V1)

方差

iris %>% 
select(-Species) %>%
summarise_all(., var, na.rm = TRUE) %>%
t() %>%
as.data.frame() %>%
rownames_to_column("Name") %>%
rename(Variance = V1)

最佳答案

iris %>% 
select(-Species) %>%
summarise_all(list(mean = mean,sd = sd, var = var), na.rm = TRUE)%>%
pivot_longer(everything(), names_sep = '_', names_to = c('Name','.value'))

# A tibble: 4 x 4
Name mean sd var
<chr> <dbl> <dbl> <dbl>
1 Sepal.Length 5.84 0.828 0.686
2 Sepal.Width 3.06 0.436 0.190
3 Petal.Length 3.76 1.77 3.12
4 Petal.Width 1.20 0.762 0.581

关于r - 如何创建一个函数来获取汇总统计数据作为列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70583523/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com