R - dplyr sum and hold other columns

I am grouping the data and then summing it up, but would also like to keep another column. I do not need to make any evaluations of the contents of this column, since it will always be the same as the group_by column. I can add it to the group_by statement, but this does not seem to be "correct". I want to save State.Full.Nameafter grouping State. Thanks

TDAAtest <- data.frame(State=sample(state.abb,1000,replace=TRUE))
TDAAtest$State.Full.Name <- state.name[match(TDAAtest$State,state.abb)]


TDAA.states <- TDAAtest %>%
  filter(!is.na(State)) %>%
  group_by(State) %>%
  summarize(n=n()) %>%
  ungroup() %>%
  arrange(State)
+4
source share
1 answer

Maybe we need

TDAAtest %>% 
     filter(!is.na(State)) %>%
     group_by(State) %>% 
     summarise(State.Full.Name = first(State.Full.Name), n = n())

Or use mutateto create a column and then dodistinct

TDAAtest %>% f
     filter(!is.na(State)) %>%
     group_by(State) %>% 
     mutate(n= n()) %>% 
     distinct(State, .keep_all=TRUE)
+8
source

Source: https://habr.com/ru/post/1652129/


All Articles