【发布时间】:2018-01-12 02:15:33
【问题描述】:
我正在尝试根据年、月或日期级别汇总利润。我正在从另一个文件中读取聚合级别,并希望将其传递给聚合函数,该文件中的值,但它抛出了一个错误。
library(lubridate)
parameter <- read.csv("Parameter.csv",header = F,col.names = c("Option","Value"))
head(parameter)
orders <- read.csv("Orders_Data.csv")
str(orders)
orders$Order.Date <- as.POSIXct(orders$Order.Date,format ="%m/%d/%Y")
orders$month = months(orders$Order.Date)
orders$Year <- year(orders$Order.Date)
head(orders$Year)
option = as.character(parameter[1,2]) #option holds the level of aggregate
option
#[1] "Day"
aggregate(Profit ~ Category + option ,data = orders, sum)
错误是
Error in model.frame.default(formula = Profit ~ Category + option, data = orders) :
variable lengths differ (found for 'option')
这是可重现的数据
option = "Year"
aggregate(Profit ~ Category + option ,data = orders, sum)
example = data.frame(date = sample(seq(as.Date('1999/01/01'), as.Date('2000/01/01'), by="day"), 24)
,Profit = sample(seq(-200,1200),24)
, Department = sample(LETTERS[seq( from = 1, to = 26 )],24))
example$Year <- year(example$date)
head(example)
aggregate(Profit ~ Department + option,data = example, sum)
还是一样的错误
【问题讨论】:
-
请提供一个可重复的小例子和预期的输出。如果您使用单个元素
option,它将无法正常工作。您可能在数据集中需要它
标签: r date statistics aggregate