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

Joshua Wiley jwiley.psych at gmail.com
Wed Sep 22 06:07:39 CEST 2010


Hi,

The problem has to do with names.  You have a dataset called "slugs",
inside this dataset is a column also called "slugs".  Now, you
attach() the slugs dataset, which adds its contents to the search
path, but it adds it *after* the global environment (your workspace)
where the dataset "slugs" is located.  In short, you are trying to
plot the column "product" and the dataset "slugs".  You can change one
of the names, or just type it out:

plot(as.factor(slugs$product), slugs$slugs)

For some more details on this take a look at:

?attach
and you can also see this by typing:

ls() #lists objects in global environment
search() # lists whats in the search path (global environment first)

HTH,

Josh

On Tue, Sep 21, 2010 at 8:27 PM, klsk89 <karenklsk89 at yahoo.com> 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.
>
> 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.
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/



More information about the R-help mailing list