under what circumstances does R recycle? -
i have 2 variables, x (takes in 5 values) , y (takes in 11 values). when want run argument,
> v <- 2*x +y +1
r responds:
error @ 2* x+y: longer object length not multiple of shorter object length.
i tried: 1*x gives me 5 values of x, y has 11 values. r says can’t add 11 5 values? – raises general question: under circumstances recycling work?
recycling works in example:
> x <- seq(5) > y <- seq(11) > x+y [1] 2 4 6 8 10 7 9 11 13 15 12 warning message: in x + y : longer object length not multiple of shorter object length > v <- 2*x +y +1 warning message: in 2 * x + y : longer object length not multiple of shorter object length > v [1] 4 7 10 13 16 9 12 15 18 21 14
the "error" reported in fact "warning" means r notifying recycling recycles anyway. may have options(warn=2)
turned on, converts warnings error messages.
in general, avoid relying on recycling. if in habit of ignoring warnings, day bite , code fail in hard diagnose way.
Comments
Post a Comment