简体   繁体   中英

Compute grouped mean while retaining single-row group in R (dplyr)

I'm trying to compute mean + standard deviation for a dataset. I have a list of organizations, but one organization has just one single row for the column "cpue." When I try to compute the grouped mean for each organization and another variable (scientific name), this organization is removed and yields a NA. I would like to retain the single-group value however, and for it to be in the "mean" column so that I can plot it (without sd). Is there a way to tell dplyr to retain groups with a single row when calculating the mean? Data below:

  l<-  df<- data.frame(organization = c("A","B", "B", "A","B", "A", "C"),
             species= c("turtle", "shark", "turtle", "bird", "turtle", "shark", "bird"),
             cpue= c(1, 2, 1, 5, 6, 1, 3))

  l2<- l %>% 
       group_by( organization, species)%>%
       summarize(mean= mean(cpue),
                 sd=sd(cpue))

Any help would be much appreciated!

We can create an if/else condition in sd to check for the number of rows ie if n() ==1 then return the 'cpue' or else compute the sd of 'cpue'

library(dplyr)
l1 <-  l %>% 
   group_by( organization, species)%>%
   summarize(mean= mean(cpue),
             sd= if(n() == 1) cpue else sd(cpue), .groups = 'drop')

-output

l1
# A tibble: 6 x 4
#  organization species  mean    sd
#* <chr>        <chr>   <dbl> <dbl>
#1 A            bird      5    5   
#2 A            shark     1    1   
#3 A            turtle    1    1   
#4 B            shark     2    2   
#5 B            turtle    3.5  3.54
#6 C            bird      3    3   

If the condition is based on the value of grouping variable 'organization', then create the condition in if/else by extracting the grouping variable with cur_group()

l %>% 
   group_by(organization, species) %>% 
   summarise(mean = mean(cpue),
       sd = if(cur_group()$organization == 'A') cpue else sd(cpue), 
            .groups = 'drop')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM