在使用dplyr中的group_by()时保留重复条目 library(dplyr)##激活data.table库 mydataWithWeeksAndWeights%计数(satisfactionLevel)它统计所有第46周条目的satisfactionLevel。问题在于,前三排的第46周是指2016年,其余的是指2017年。我想保留这些重复的条目。
我不能确定我的代码是否符合您的要求,因为您没有给出预期的输出,但我认为您需要做的是添加一个在使用dplyr中的group_by()时保留重复条目 library(dplyr)##激活data.table库 mydataWithWeeksAndWeights%计数(satisfactionLevel)它统计所有第46周条目的satisfactionLevel。问题在于,前三排的第46周是指2016年,其余的是指2017年。我想保留这些重复的条目。,r,dplyr,data-manipulation,R,Dplyr,Data Manipulation,我不能确定我的代码是否符合您的要求,因为您没有给出预期的输出,但我认为您需要做的是添加一个年列,并通过将其添加到组中,以便区分2016年第46周和2017年第46周 编辑:如果您需要自动定义从您拥有的结束日期开始的年份,我将在@docendodiscimus的注释中添加位: library(dplyr) ##activates the data.table library mydataWithWeeksAndWeights <- data_frame(ended = c("14/11/2
年
列,并通过将其添加到组中,以便区分2016年第46周和2017年第46周
编辑:如果您需要自动定义从您拥有的结束日期开始的年份,我将在@docendodiscimus的注释中添加位:
library(dplyr) ##activates the data.table library
mydataWithWeeksAndWeights <- data_frame(ended = c("14/11/2016",
"14/11/2016",
"14/11/2016",
"02/01/2017",
"02/01/2017",
"15/11/2017",
"15/11/2017",
"16/11/2017",
"16/11/2017"),
week = c(46, 46, 46, 1, 1, 46, 46, 46, 46),
satisfactionLevel = c("Very dissatisfied",
"Very satisfied",
"Satisfied",
"Dissatisfied",
"Very dissatisfied",
"Very satisfied",
"Very dissatisfied",
"Very Satisfied",
"Very satisfied"),
weight = c(0, 1, 0.75, 0.25, 0, 1, 0, 1, 1))
库(dplyr)
mydataWithWeeksAndWeights下面是我要做的:将“ended”格式重新格式化为日期格式,并使用聚合函数:
library(dplyr)
mydataWithWeeksAndWeights <- data_frame(ended = c("14/11/2016",
"14/11/2016",
"14/11/2016",
"02/01/2017",
"02/01/2017",
"15/11/2017",
"15/11/2017",
"16/11/2017",
"16/11/2017"),
week = c(46, 46, 46, 1, 1, 46, 46, 46, 46),
satisfactionLevel = c("Very dissatisfied",
"Very satisfied",
"Satisfied",
"Dissatisfied",
"Very dissatisfied",
"Very satisfied",
"Very dissatisfied",
"Very Satisfied",
"Very satisfied"),
weight = c(0, 1, 0.75, 0.25, 0, 1, 0, 1, 1))
mydataWithWeeksAndWeights$year <- format(as.Date(mydataWithWeeksAndWeights$ended,
"%d/%m/%Y"), "%Y")
pivotTable <- mydataWithWeeksAndWeights %>%
group_by(week, year, weight) %>%
count(satisfactionLevel)
#只是为了缩短df名称
df TrymydataWithWeeksAndWeights%%>%groupby(week,weight)%%>%filter(n()>1)
我想你应该区分年份。您可以使用df%>%groupby(year\u week=format(截至日期(结束,“%d/%m/%Y”)、%Y-%W)、重量)%%>%count(满意度水平)
@docendodiscimus!!!Cheers@docendodiscimus你能提交你的评论作为答案吗?@greeconomist它已经以类似的方式在现有答案中实现了
# just to shorten df-name
df <- mydataWithWeeksAndWeights
# reformat and add column with year
df[,"ended"] <- as.Date(df[[1]], format = "%d/%m/%Y")
df$year <- format(df[[1]], "%Y")
# actual aggregating
aggregate (df$weight, by = list(df$year, df$satisfactionLevel, df$week), FUN = sum)