0

I'm trying to sum PONDERA when ESTADO==1 and then group by AGLOMERADO

new <- recorte %>% group_by(AGLOMERADO) %>%
                        summarise(TOTocupied=sum(recorte[recorte$ESTADO==1,"PONDERA"]))

The sum is working correctly, but I can't get the result to be grouped by AGLOMERADO, it gives me back the same result for each AGLOMERADO:

AGLOMERADO  TOTocupied
1           100
2           100
3           100

What am I doing wrong?

juanmac
  • 121
  • 1
  • 12

1 Answers1

0

Don't use $ in dplyr pipe. Also no need to refer to the dataframe again since we are using pipes.

You can try -

library(dplyr)

new <- recorte %>% 
         group_by(AGLOMERADO)%>%
         summarise(TOTocupied = sum(PONDERA[ESTADO==1], na.rm = TRUE))
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213