Priit Mets
Priit Mets

Reputation: 495

Why I get the same value when computing mean and sum in R?

I have the following dataset:

structure(list(season = c("winter", "winter", "winter", "winter", 
"winter", "winter", "winter", "winter", "winter", "winter", "winter", 
"winter", "winter", "winter", "winter", "winter", "winter", "winter", 
"winter", "winter", "winter", "winter", "winter", "winter", "winter", 
"winter", "winter", "winter", "winter", "winter", "winter"), 
    monthtrunc = structure(c(1606773600, 1606773600, 1606773600, 
    1606773600, 1606773600, 1606773600, 1606773600, 1606773600, 
    1606773600, 1606773600, 1606773600, 1606773600, 1606773600, 
    1606773600, 1606773600, 1606773600, 1606773600, 1606773600, 
    1606773600, 1606773600, 1606773600, 1606773600, 1606773600, 
    1606773600, 1606773600, 1606773600, 1606773600, 1606773600, 
    1606773600, 1606773600, 1606773600), class = c("POSIXct", 
    "POSIXt"), tzone = ""), HBALNOSE = c(22.4303, 22.4303, 22.4303, 
    22.4303, 22.4303, 22.4303, 22.4303, 20.3472, 20.3472, 20.3472, 
    20.3472, 20.3472, 20.3472, 20.3472, 21.5194, 21.5194, 21.5194, 
    21.5194, 21.5194, 21.5194, 21.5194, 23.4169, 23.4169, 23.4169, 
    23.4169, 23.4169, 23.4169, 23.4169, 20.5736, 20.5736, 20.5736
    )), row.names = 2160:2190, class = "data.frame")

Then I use following code to get mean and sum values, however they are the same. Where is the error?

a%>%group_by(season,monthtrunc)%>%
  summarise(

    #weather
    HBALNOSE = sum(HBALNOSE, na.rm = TRUE),  

    HBALNOSEmean = mean(HBALNOSE, na.rm = TRUE) 

  )

Upvotes: 0

Views: 205

Answers (1)

akrun
akrun

Reputation: 887961

The reason is that it is assigning the column HBALNOSE with the sum of 'HBALNOSE'. Now, there is only the sum value available which is a single value and thus the mean is also the same

library(dplyr)
a%>%
  group_by(season, monthtrunc)%>%
   summarise(


   HBALNOSE_sum = sum(HBALNOSE, na.rm = TRUE),  

   HBALNOSEmean = mean(HBALNOSE, na.rm = TRUE) 

)

As there is only a single group, it can be checked as well

sum(a$HBALNOSE)
#[1] 675.7174
mean(a$HBALNOSE)
#[1] 21.79734

Upvotes: 4

Related Questions