• Register
0 votes
1.8k views

Problem :

I am facing below error and I am unable to understand the meaning of the error. All my variable names are correct and there are no typing errors. Does anybody faced this issue before any solution on it?

My erroneous code :

datNewagg <- aggregate (dataNew, by = list('x', 'y', 'z', 'a', 'ab'), FUN = mean)  

It gives following error :

Error in aggregate.data.frame(datNew, by = list("x", "y",  : arguments must have same length
6 5 3
7,540 points

Please log in or register to answer this question.

2 Answers

0 votes

Solution :

Assuming these are not a typing mistakes the x, y, z, a and ab are the names of columns in the dataNew.

Some functions such as subset will let you specify the column names of their objects directly. But the aggregate function does not allow that. So the columns of your dataNew which are listed in the by argument must be clearly referred to as such as given in blow code snippet:

datNewagg <- aggregate(dataNew,
    by = list(
        x = dataNew$x,
        y = dataNew$y,
        z = dataNew$z,
        a = dataNew$a,
        ab = dataNew$ab),
    FUN = mean) 

 

OR

By using the data.frame as the by argument works for me try following :

datNewagg <- aggregate (dataNew, by = dataNew[c('x', 'y', 'z', 'a', 'ab')], FUN = mean)

I want to say please do not give the by argument, just give the name of the arguments and also give the data.frame with columns as these arguments.

9 7 4
38,600 points
0 votes

Solution:

Few functions, like subset, will approve you to seclude column names of the object they're performing on directly. The aggregate function doesn't, hence any columns of dataNew listed in the by argument require to particularly referred to as such. Attempt this:

datNewagg <- aggregate(dataNew,
    by = list(
        x = dataNew$x,
        y = dataNew$y,
        z = dataNew$z,
        a = dataNew$a,
        ab = dataNew$ab),
    FUN = mean) 

I used obtain this error.
The easy solution to remove this error is to write all the variables along with their dataset name like "ds_name$var_name".
I'm not certain what is the dataset name is your case, so I'll give you another same example.

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.

Check class(dataNew). In case it's not a data.frame, this dataNew <- data.frame(dataNew) prior aggregation must solve the error or

datNewagg <- aggregate (data.frame(dataNew), by = list('x', 'y', 'z', 'a', 'ab'),

You can inspect in case aggregate method is supported by methods().

# stars 0.2-0
> library(stars)
> methods(class= "stars")
 [1] [              [<-            adrop          aperm          as.data.frame  c              coerce        
 [8] cut            dim            dimnames       dimnames<-     filter         image          initialize    
[15] is.na          Math           merge          Ops            plot           print          show          
[22] slotsFromS3    split          st_apply       st_as_sf       st_as_sfc      st_as_stars    st_bbox       
[29] st_coordinates st_crop        st_crs         st_crs<-       st_dimensions  st_transform   st_write  

Ta-da! stars 0.3-0 is on CRAN today (25/FEB/2019).

# stars 0.3-0
> library(stars)
> methods(class= "stars")
 [1] $<-            [              [<-            adrop          aggregate      aperm         
 [7] as.data.frame  c              coerce         cut            dim            dimnames      
[13] dimnames<-     image          initialize     is.na          Math           merge         
[19] Ops            plot           print          show           slotsFromS3    split         
[25] st_apply       st_area        st_as_sf       st_as_sfc      st_as_stars    st_bbox       
[31] st_coordinates st_crop        st_crs         st_crs<-       st_dimensions  st_geometry   
[37] st_redimension st_transform   write_stars 

You can view aggreagte is listed. 

By the way, in case you include tidyverse, the list expands like:

> library(tidyverse)
> library(stars)
> methods(class= "stars")
 [1] $<-            [              [<-            adrop          aggregate      aperm         
 [7] as.data.frame  as.tbl_cube    c              coerce         cut            dim           
[13] dimnames       dimnames<-     filter         image          initialize     is.na         
[19] Math           merge          mutate         Ops            plot           print         
[25] pull           select         show           slice          slotsFromS3    split         
[31] st_apply       st_area        st_as_sf       st_as_sfc      st_as_stars    st_bbox       
[37] st_coordinates st_crop        st_crs         st_crs<-       st_dimensions  st_geometry   
[43] st_redimension st_transform   write_stars   

 

10 6 4
31,120 points

Related questions

0 votes
1 answer 4 views
4 views
Problem: Any solution for this error: Error in aggregate.data.frame(as.data.frame(x), ...) : arguments must have same length
asked Mar 14 Wafa Abu Yousef 6.1k points
0 votes
1 answer 2 views
2 views
Problem: Somone please help me to fix this error in unit(tic_pos.c, "mm") : 'x' and 'units' must have length > 0
asked 2 days ago padma1 22.4k points
1 vote
1 answer 1.2K views
1.2K views
Problem: I have limited knowledge of R and ggplot. I have recently written a function and trying to run the function as shown below: ggplot(datfr, aes(x = dat1[1:951,], y = dat2[1:951,])) + geom_point() After executing above function I am facing below error: Don't ... and when I try to use nrow on my two datasets it again returns the same number of rows. How can I fix above ggplot related error.
asked May 26, 2020 Martin K 6.6k points
0 votes
1 answer 4 views
4 views
Problem: i don&rsquo;t understand this problem. please help me? names' attribute [2] must be the same length as the vector [1]
asked 5 days ago Rohit kr 16.2k points
1 vote
1 answer 6 views
6 views
Problem : The Query works fine under Sybase, but gives the error in the title of this post when executed under SQL Server.General outline of the query is: select sum ( t.graduates - ( select sum ( t1.graduates ) from table as t1 where t1 ... : SQL Server &ldquo;cannot perform an aggregate function on an expression containing an aggregate or a subquery&rdquo; Any suggestions would be appreciated.
asked Mar 30 Shovo210 13.8k points
0 votes
1 answer 333 views
333 views
Problem : I want to use the TraMineR's (version 1.8.4) seqdef-funciton to define the sequence object, however I always face the following error message which is completely senseless to me: Error in row.names<-.data.frame(*tmp*, value = value) : invalid 'row. ... tried running it after re-labeling the states without "-", but it does not affect the error. Does someone knows what causes this error?
asked Dec 6, 2019 alecxe 7.5k points
0 votes
1 answer 2.6K views
2.6K views
Problem : I have been facing below error message and traceback: Error in FUN(X[[i]], ...) : only defined on the data frame with all numeric variables 5 stop("only defined on the data frame with all numeric variables") 4 FUN(X[[i]], ...) 3 lapply(args, ... .numeric(data[good,]) But now I get the different error message back as below : "Error: (list) object cannot be coerced to type 'double'"
asked Dec 16, 2019 alecxe 7.5k points
0 votes
1 answer 1.1K views
1.1K views
Problem : I am trying to merge data in R but facing following error error in fix.by(by.x, x) : 'by' must specify one or more columns as numbers, names or logical
asked Nov 15, 2019 peterlaw 6.9k points
0 votes
2 answers 183 views
183 views
Problem : I am stuck because of following weired error error in sort.list(y) : 'x' must be atomic for 'sort.list' have you called 'sort' on a list?
asked Nov 15, 2019 peterlaw 6.9k points
0 votes
1 answer 267 views