0

I have the following dataframe.

Name <- c("John","Mark","Ella","Mike","Zedd","John","Maria","Nick","John","Nick","Zedd","Andrea")
Age <- c(16,25,45,23,26,28,19,20,43,31,33,15)
df <- data.frame(Name,Age)

I want to group by name and sum the years of age. This what I am using without success.

library(dplyr)
new_df <- group_by(Name) %>% summarise(Years = sum(Age)), count = count(Name))
halfer
  • 19,824
  • 17
  • 99
  • 186
CS.py
  • 43
  • 6

1 Answers1

1

Do you mean:

library(dplyr

df <- data.frame(Name,Age) %>% 
  group_by(Name) %>% 
  summarise(Years = sum(Age, rm.na = TRUE),
            count = n())


Peter
  • 11,500
  • 5
  • 21
  • 31