0

I don't really even know how to describe what is happening, other than that R knows the value of a variable, but then is applying it incorrectly inside a function. This is copied directly from my terminal:

> n2
[1] 4

> rnorm(n2)
[1]  0.379217 -1.519947  1.602527

> rnorm(4)
[1] -0.93154715 -0.82908620  0.55084001  0.09018763

R knows n2 = 4, and it knows how to deal with rnorm(4), but somehow rnorm(n2) is being read as rnorm(3).

Below is my full code. I set R to interpret a warning as an error to stop the loop where the problem occurs. After running this check the value of n2, then see what rnorm(n2) does.

rm(list = ls())
options(warn = 2)
iterations = 1
n = 10
proportions = seq(.3, .7, by = .1)
#stores
y = c()

position = 1

#set var ratio
parameter = 1.25
#loop over proportions
for(p in proportions){
  n1 = p*n
  n2 = n-n1
  for(i in 1:iterations){
    #new ys
    newy1 = sqrt(parameter)*rnorm(n1)
    newy0 = rnorm(n2)
    newy = c(newy1, newy0)
    y[position:(position+n-1)] = newy


    position = position+n
  }
}

I am running R 3.2.3 on Windows 7 64 bit.

Sorry it took me a bit to get the question straightened out. Long time user, first time poster.

Thanks

  • Welcome to SO (and R)! When asking questions here, it is strongly encouraged that you provide [minimal questions](http://stackoverflow.com/help/mcve) and [reproducible examples](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). It is also recommended that, especially in cases like this where it is unclear, *what is the expected output*? This is behaving correctly and, to me at least, intuitively. – r2evans Mar 23 '16 at 23:03
  • So, if `n2==3`, what would you expect `rnorm(n2)` to be if not `rnorm(3)`? – kdopen Mar 23 '16 at 23:14
  • Sorry, if you look at the code n2 is actually 4. I accidentally said n2 = 3 when commenting on it. I fixed that. – user6106913 Mar 23 '16 at 23:20
  • What you show is obviously flawed, so perhaps there is (1) a misunderstanding on your part, (2) a typo in what you have asked, or (3) your R is fundamentally dorked (in a way that I have never seen). Close R, start it again, type in the code above (after the line(s) you are not showing us) and report back. – r2evans Mar 23 '16 at 23:23
  • I added the full code for clarity. – user6106913 Mar 23 '16 at 23:46
  • I closed and reopened R. Same problem. Then I upgraded from 3.2.3 to 3.2.4. Problem persists. – user6106913 Mar 23 '16 at 23:53
  • You reference an error but do not show us what it is. Please realize that people volunteer their time to help on this site; consider making this easy to support a "drive by answer", meaning we don't have to do a whole lot of typing, executing, etc, in order to extract what you should be providing in the first place. – r2evans Mar 23 '16 at 23:54
  • I expect rnorm(n2) to return 4 random normal variables when n2 == 4. It is instead returning 3 of them. – user6106913 Mar 23 '16 at 23:57

1 Answers1

1

For clarity to others coming to this page: the code above results in an error:

Error in y[position:(position + n - 1)] = newy (from #9) : 
  (converted from warning) number of items to replace is not a multiple of replacement length

This is because the length of the left side of the assignment is not the same as the right:

length(position:(position+n-1))
# [1] 10
length(newy)
# [1] 9

Tracing back through the code, what is causing newy to be shorter?

lengths(list(newy1, newy0))
# [1] 6 3
n2
# [1] 4

The "3" is the culprit, it should be "4". This is very specifically R FAQ 7.31:

n2 == 4
# [1] FALSE
n2 - 4
# [1] -8.881784e-16

Perhaps you should consider using round, floor, ceiling, as.integer, or some other function when you assign n1 = p*n (since p is fractional).

One problem with this phenomenon is that it doesn't always mis-perform.

r2evans
  • 141,215
  • 6
  • 77
  • 149