I keep getting this error and I'm not quite sure what it means. All of my variable names are consistent and there are no typos. Am I missing something here?
我一直在犯这个错误,我不太清楚这是什么意思。我所有的变量名都是一致的,没有拼写错误。我是不是漏掉了什么?
The code
的代码
datNewagg <- aggregate (dataNew, by = list('x', 'y', 'z', 'a', 'ab'),
FUN = mean)
Produces the error
产生的误差
Error in aggregate.data.frame(datNew, by = list("x", "y", :
arguments must have same length
5 个解决方案
#1
7
Assuming it's not a typo (the data frame is called dataNew
in your call but datNew
in the error), are x
, y
, z
, a
and ab
the names of columns in dataNew
?
假设它不是一个typo(数据帧在您的调用中被称为datNew,但在错误中是datNew),那么x、y、z、a和ab都是datNew中列的名称吗?
Some functions, like subset
, will allow you to specify column names of the object they're working on directly. The aggregate
function doesn't, so any columns of dataNew
listed in the by
argument need to specifically referred to as such. Try this:
一些函数,比如子集,将允许您指定它们正在处理的对象的列名称。聚合函数没有,因此,通过参数中列出的任何数据列都需要特别地引用。试试这个:
datNewagg <- aggregate(dataNew,
by = list(
x = dataNew$x,
y = dataNew$y,
z = dataNew$z,
a = dataNew$a,
ab = dataNew$ab),
FUN = mean)
#2
4
I used get this error.
The simple solution to remove this error is to write all the variables along with their dataset name like "ds_name$var_name".
I'm not sure what is the dataset name is your case, so I'll give you another similar example.
我用到了这个错误。删除此错误的简单解决方案是将所有变量连同其数据集名称(如“ds_name$var_name”)一起写入。我不确定数据集的名称是什么,我再给你们一个类似的例子。
curYearRev <-aggregate(hr$Year.Total, by = list(hr$Hospital_ID,hr$District_ID,hr$Instrument_ID) , FUN = sum)
Here, "hr" is the dataset name and "Year.Total", "Hospital_ID", "District_ID", "Instrument_ID" are the variables in "hr" dataset.
这里,“hr”是数据集名称和“年份”。“Total”、“Hospital_ID”、“loct_id”、“servicent_id”是“hr”数据集中的变量。
Writing aggregate functions in this way will never give you any errors again.
以这种方式编写聚合函数将不会再次出现任何错误。
#3
1
Check class(dataNew)
. If it's not a data.frame, this dataNew <- data.frame(dataNew)
before aggregation should solve the error or
检查类(dataNew)。如果它不是一个数据。frame,这个datnew <- data.frame(datnew)在聚合之前应该解决这个错误。
datNewagg <- aggregate (data.frame(dataNew), by = list('x', 'y', 'z', 'a', 'ab'),
FUN = mean)
#4
0
Using data.frame
as by
argument works for me try this:
使用data.frame作为参数对我来说是这样的:
datNewagg <- aggregate (dataNew, by = dataNew[c('x', 'y', 'z', 'a', 'ab')], FUN = mean)
I mean don't give the by
argument, just the name of the arguments, give a data.frame with columns as these arguments
我的意思是,不要给出参数,只给出参数的名称,给出一个带有列的数据。
#5
0
When you use with(...,aggregate(...))
don't put your column names in quotes.
当您使用(…,聚合(…))时,不要将列名放在引号中。
#1
7
Assuming it's not a typo (the data frame is called dataNew
in your call but datNew
in the error), are x
, y
, z
, a
and ab
the names of columns in dataNew
?
假设它不是一个typo(数据帧在您的调用中被称为datNew,但在错误中是datNew),那么x、y、z、a和ab都是datNew中列的名称吗?
Some functions, like subset
, will allow you to specify column names of the object they're working on directly. The aggregate
function doesn't, so any columns of dataNew
listed in the by
argument need to specifically referred to as such. Try this:
一些函数,比如子集,将允许您指定它们正在处理的对象的列名称。聚合函数没有,因此,通过参数中列出的任何数据列都需要特别地引用。试试这个:
datNewagg <- aggregate(dataNew,
by = list(
x = dataNew$x,
y = dataNew$y,
z = dataNew$z,
a = dataNew$a,
ab = dataNew$ab),
FUN = mean)
#2
4
I used get this error.
The simple solution to remove this error is to write all the variables along with their dataset name like "ds_name$var_name".
I'm not sure what is the dataset name is your case, so I'll give you another similar example.
我用到了这个错误。删除此错误的简单解决方案是将所有变量连同其数据集名称(如“ds_name$var_name”)一起写入。我不确定数据集的名称是什么,我再给你们一个类似的例子。
curYearRev <-aggregate(hr$Year.Total, by = list(hr$Hospital_ID,hr$District_ID,hr$Instrument_ID) , FUN = sum)
Here, "hr" is the dataset name and "Year.Total", "Hospital_ID", "District_ID", "Instrument_ID" are the variables in "hr" dataset.
这里,“hr”是数据集名称和“年份”。“Total”、“Hospital_ID”、“loct_id”、“servicent_id”是“hr”数据集中的变量。
Writing aggregate functions in this way will never give you any errors again.
以这种方式编写聚合函数将不会再次出现任何错误。
#3
1
Check class(dataNew)
. If it's not a data.frame, this dataNew <- data.frame(dataNew)
before aggregation should solve the error or
检查类(dataNew)。如果它不是一个数据。frame,这个datnew <- data.frame(datnew)在聚合之前应该解决这个错误。
datNewagg <- aggregate (data.frame(dataNew), by = list('x', 'y', 'z', 'a', 'ab'),
FUN = mean)
#4
0
Using data.frame
as by
argument works for me try this:
使用data.frame作为参数对我来说是这样的:
datNewagg <- aggregate (dataNew, by = dataNew[c('x', 'y', 'z', 'a', 'ab')], FUN = mean)
I mean don't give the by
argument, just the name of the arguments, give a data.frame with columns as these arguments
我的意思是,不要给出参数,只给出参数的名称,给出一个带有列的数据。
#5
0
When you use with(...,aggregate(...))
don't put your column names in quotes.
当您使用(…,聚合(…))时,不要将列名放在引号中。