[R] strange behaviour with loops and lists
Liviu Andronic
landronimirc at gmail.com
Sun May 19 18:08:52 CEST 2013
Dear all,
I encountered this strange behaviour with loops and lists. Consider this:
xl <- list()
for(i in 5:7){##loop over numeric vector
xl[[i]] <- rnorm(i)
}
> xl
[[1]]
NULL
[[2]]
[1] -0.4448192 -1.3395014
[[3]]
[1] 1.3214195 -1.2968560 -0.6327795
The above lists contained a NULL element for some reason. While the code below:
xl <- list()
for(i in as.character(2:3)){##loop over character vector
xl[[i]] <- rnorm(i)
}
> xl
$`2`
[1] -1.139506 2.894280
$`3`
[1] 0.0599175 1.0793515 0.4296049
This resulting list contains no extraneous elements. Is this normal? Why?
Thanks,
Liviu
--
Do you know how to read?
http://www.alienetworks.com/srtest.cfm
http://goodies.xfce.org/projects/applications/xfce4-dict#speed-reader
Do you know how to write?
http://garbl.home.comcast.net/~garbl/stylemanual/e.htm#e-mail
More information about the R-help
mailing list