Error in xy.coords(x, y) : 'x' and 'y' lengths differ

hey m new kindly help
set.seed(15) # for generation of random online customers
x=runif(10,0,10) # random x coord
y=runif(10,0,10) # random y coord

onlineloc=cbind(x,y)
plot(x,y,main="Online customer loc",col=" red")
a=runif(10,0,10)
b=runif(10,0,10)
points(x[b],y[a],pch=2)

Hi.

a and b are numeric with decimals. You cannot use them for indexiong in points(x[b],y[a],pch=2). a and b need to be integers.

1 Like

m saving all as integer but result is same pl help
set.seed(15)
options(digits = 1)# for generation of random online customers
x=runif(10,0,15) # random x coord
y=runif(10,0,15) # random y coord
a=runif(10,0,15)
b=runif(10,0,15)
x=as.integer(x)
y=as.integer(y)
a=as.integer(a)
b=as.integer(b)
#onlineloc=cbind(x,y)
plot(x,y,main="Online customer loc",col=" red")
points(x[b],y[a],pch=2)

Your new code works in my laptop.

My sessioninfo:

R version 3.4.4 (2018-03-15)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Linux Mint 18.3

Matrix products: default
BLAS: /usr/lib/openblas-base/libblas.so.3
LAPACK: /usr/lib/libopenblasp-r0.2.18.so

locale:
 [1] LC_CTYPE=en_AU.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_AU.UTF-8        LC_COLLATE=en_AU.UTF-8    
 [5] LC_MONETARY=en_AU.UTF-8    LC_MESSAGES=en_AU.UTF-8   
 [7] LC_PAPER=en_AU.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_AU.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
[1] compiler_3.4.4


sir how i can compare 10 random locations(set no 1) with other 4 random locations(set no 2) having xy coordinates of both sets keeping in mind one restriction that comparison or matching is done only if set 1 locations enroute set 2 locations or otherwise kindly help

This doesn't work in my system.

sessionInfo
> sessionInfo()
R version 3.6.1 (2019-07-05)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18362)

Matrix products: default

locale:
[1] LC_COLLATE=English_India.1252  LC_CTYPE=English_India.1252   
[3] LC_MONETARY=English_India.1252 LC_NUMERIC=C                  
[5] LC_TIME=English_India.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods  
[7] base     

loaded via a namespace (and not attached):
[1] compiler_3.6.1 tools_3.6.1

This happens because as.integer just truncates the fractional part, and for the particular seed value of 15, one value of a (the 4th one) becomes 0. Since indexing starts from 1 in R, it doesn't return anything and hence the length varies in between x[a] and y[b].

I think this works in Fer's system because of the old version. The algorithm of random number generation changed in a recent update, though I haven't noted carefully which are the functions are affected by that change (I thought only sample, but apparently that's not the case).

@Emmadadil, I didn't really follow what you mean. If you just want to plot the original points in "red", and some random points from the cartesian product of the co-ordinates, you can do something like this:

set.seed(seed = 15)

x <- runif(n = 10,
           min = 0,
           max = 15)
y <- runif(n = 10,
           min = 0,
           max = 15)

plot(x = x,
     y = y,
     main = "Online customer loc",
     col = "red")
points(x = sample(x = x,
                  size = 10,
                  replace = TRUE),
       y = sample(x = y,
                  size = 10,
                  replace = TRUE),
       pch = 2)

This generates the following plot:

image

Please customise the plot as per your requirement.

Hope this helps.

3 Likes

how i can use 2D array in R is wat is the syntax please guide.
a=runif(15,0,15)
b=runif(15,0,15)
for(i in a ,j in b){
print(i,j)
}

How to compare two random locations having x and y coordinates in Rstudio

Hi,

In order for us to help you with your question, please provide us a minimal reprocudible example where you provide a minimal (dummy) dataset and code that can recreate the issue. One we have that, we can go from there. For help on creating a Reprex, see this guide:

Good luck!
PJ

sir how i can compare 10 random locations(set no 1) with other 4 random locations(set no 2) having xy coordinates of both sets keeping in mind one restriction that comparison or matching is done only if set 1 locations enroute set 2 locations or otherwise kindly help i mean wat function i can use for this problem in R no such code is available as yet is there any literature available in R for such problems

That's rigth. I just checked in a docker container with R 3.6.1 and I cannot make it (I decided not to upgrade or touch anything until my phd is submitted, which apparently will happen in the next 10 days :blush: ).

The change of the random number generator impacted any function with such properties, like those linked with simulating any distribution (i.e. rnorm...). Essentially, it broke any test for packages, functiones using set.seed to check reproducibility, anything involving bayesian statistics (that's my case, so I didn't upgrade)...

I actually run the code provided without the seed, and I see from time to time it fails, so it certainly is because of what you pointed that as.integer makes zeroes if the double is something like 0.342345
cheers

Note that you can use an earlier RNG version for reproducibility via RNGversion():
https://stat.ethz.ch/R-manual/R-devel/library/base/html/Random.html

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.