summarise()是否保留其他未汇总的列?

yb3bgrhw  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(90)

我试图按照这篇文章https://www.datanovia.com/en/lessons/ggplot-error-bars/做一个带有误差线的图。但是总结不太起作用。为什么?

> df$dose <- as.factor(df$dose)
> head(df, 3)
   len supp dose
1  4.2   VC  0.5
2 11.5   VC  0.5
3  7.3   VC  0.5
> df.summary <- df %>%
+     group_by(dose) %>%
+     summarise(
+         sd = sd(len, na.rm = TRUE),
+         len = mean(len)
+     )
> df.summary
        sd      len
1 7.649315 18.81333

字符串
但看起来你应该得到这样的东西:

## # A tibble: 3 x 3
##   dose     sd   len
##   <fct> <dbl> <dbl>
## 1 0.5    4.50  10.6
## 2 1      4.42  19.7
## 3 2      3.77  26.1


尝试了第一个答案,但仍然不起作用

> df <- data.frame(len = runif(30, 4.2, 11.5), supp = "VC", dose = sample(c(.5, 1, 2), 30, replace = TRUE))
> df.summary <- df %>%
+     group_by(dose) %>%
+     summarise(
+         sd = sd(len, na.rm = TRUE),
+         len = mean(len)
+     )
> df.summary
        sd      len
1 2.385391 8.057899
> dput(df[1:10,])
structure(list(len = c(6.38568101720884, 9.66049139532261, 6.71973214379977, 
9.11211366723292, 10.07433216176, 8.6760600333102, 5.71324644873384, 
4.77394210803322, 10.921557949041, 4.29420428201556), supp = c("VC", 
"VC", "VC", "VC", "VC", "VC", "VC", "VC", "VC", "VC"), dose = c(2, 
1, 2, 0.5, 1, 2, 0.5, 1, 1, 1)), row.names = c(NA, 10L), class = "data.frame")

z8dt9xmd

z8dt9xmd1#

我建议你使用tidyverse加载必要的库,不要使用被取代的plyr。像这样:

library(tidyverse)

# the df in your example
df %>%
       group_by(dose) %>%
       summarise(
             sd = sd(len, na.rm = TRUE),
            len = mean(len)
         )
#it gives
# A tibble: 3 x 3
   dose    sd   len
  <dbl> <dbl> <dbl>
1   0.5  2.40  7.41
2   1    3.15  7.94
3   2    1.24  7.26

字符串

相关问题