【发布时间】:2020-04-21 21:33:28
【问题描述】:
如何更改下面代码中的组函数,使其也包含startdate 的常量值?
#Reproducing an example of what I like to have:
employee <- c('John Doe','John Doe','Peter Gynn','Peter Gynn','Jolie Hope','Jolie Hope')
startdate <- as.Date(c('2010-11-1','2010-11-1','2008-3-25','2008-3-25','2007-3-14','2007-3-14'))
salary <- c(100,200,100,300,800,12)
employ.data <- data.frame(employee, startdate, salary)
#Grouping by employee en summing salary
grouped.file <- employ.data %>% group_by(employee) %>%
summarize(salary = sum(salary, na.rm =T))
#But I would like to have a dataframe like this:
employee <- c('John Doe','Peter Gynn','Jolie Hope')
startdate <- as.Date(c('2010-11-1','2008-3-25','2007-3-14'))
salary <- c(300,400,812)
employ.data <- data.frame(employee, startdate, salary)
【问题讨论】:
标签: r dataframe dplyr grouping