[R] How to fix error: 'x' and 'y' lengths differ

David Winsemius dwinsemius at comcast.net
Wed Sep 22 06:09:24 CEST 2010


On Sep 21, 2010, at 11:27 PM, klsk89 wrote:

>
> Hi, im a student so still very new to R. Hope someone could help me  
> out here
> =)
> They are 3 slug control products, bustaslug, product X and Y. Im ask  
> to
> explore the data by plot() and tapply(). But when i try to plot or  
> use the
> tapply command, it tells me that the x and y lengths differ. so im  
> thinking
> its because the data is unbalanced? Is there a simple command that i  
> could
> use to fix this? Thanks for the help.
>
This is not a homework help site. You could try seeing if the slugs  
data.frame has the structure you expected with:

str(slugs)  # I cannot say whether the attach()-ment is going to mess  
this up because I NEVER use attach. Seems having a datafame named the  
same as a column could be a confusing strategy. In fact you may have  
provided yet another reason why attach should NEVER be used.

I made this into a dataframe called "slug":

with(slug, plot(as.factor(product),slugs))  # a perfectly fine plot

 > slugs <- slug
 > attach(slugs)
The following object(s) are masked _by_ '.GlobalEnv':

     slugs
 >  tapply(slugs,product,mean)
Error in tapply(slugs, product, mean) : arguments must have same length
 >  plot(as.factor(product),slugs)
Error in xy.coords(x, y, xlabel, ylabel, log) :
   'x' and 'y' lengths differ

OK, now I will say it again. NEVER use attach().

-- 
David.
> Heres what i did.
>
>> attach(slugs)
>> slugs
>
>    product       slugs
> 1  bustaslug     5
> 2  bustaslug     2
> 3  bustaslug     3
> 4  bustaslug     0
> 5  bustaslug     4
> 6  bustaslug     4
> 7  bustaslug     1
> 8  bustaslug     5
> 9  bustaslug     6
> 10 bustaslug     4
> 11  productX     0
> 12  productX     3
> 13  productX     4
> 14  productX     1
> 15  productX     2
> 16  productX     2
> 17  productX     4
> 18  productX     0
> 19  productX     3
> 20  productX     1
> 21  productY     7
> 22  productY     9
> 23  productY    11
> 24  productY     5
> 25  productY    13
> 26  productY     7
> 27  productY     8
> 28  productY    12
> 29  productY    10
> 30  productY    14
>
>
>> plot(as.factor(product),slugs)
> Error in xy.coords(x, y, xlabel, ylabel, log) :
>  'x' and 'y' lengths differ
>
>> tapply(slugs,product,mean)
> Error in tapply(slugs, product, mean) : arguments must have same  
> length
> -- 
> View this message in context: http://r.789695.n4.nabble.com/How-to-fix-error-x-and-y-lengths-differ-tp2549694p2549694.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list