Rename a column in a group by data.frame

Hi,

I don't know why but my columns "annee" where I use the groupby is rename in a bad version :

df <- data.frame(subset(data))[,c("nb_aaa", "nb_bbb")] %>%
  group_by(data[,c("annee")]) %>%
  summarise_each(funs = sum)

It works but my output is :
Capture d’écran 2020-10-27 à 17.45.04

how can I have only "annee" ?? and not data[,c("annee")]

Thanks

You at least want to write

group_by(annee)

I am not sure what you intend to do with

data.frame(subset(data))[,c("nb_aaa", "nb_bbb")]

Is data a data frame and you want just the columns nb_aaa and nb_bbb? If so, where will the column annee come from ?

Maybe you mean sommething like this?

library(dplyr)
DF <- data.frame(annee = rep(2012:2014, each = 3), Dummy = 0:8, nb_aaa = 1:9, nb_bbb = 11:19)
DF %>% select(annee, nb_aaa, nb_bbb) %>%  group_by(annee) %>% 
  summarize_each( funs =sum)

when I try I have that :
Capture d’écran 2020-10-28 à 10.15.14

while my table data have the column "annee" :
Capture d’écran 2020-10-28 à 10.17.54

My output is good but it's just the name of the column that is not, and i don't know how to change it..

OK it works :smiley:

I have to had "annee" in my subset(data), like this :

df <- data.frame(subset(data))[,c("annee","nb_lgt_aut_ind", "nb_lgt_aut_coll_res")] %>%
  group_by(annee) %>%
  summarise_each(funs = sum)

Thanks you :slight_smile:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.