[R] dynamic variable creation in lists and data frames
Daniel Lee Rabosky
dlr32 at cornell.edu
Tue Dec 5 20:41:40 CET 2006
Hi
I have a question about the creation of variables within lists in R. I am
running simulations and am interested in two parameters, ESM and ESMM (the
similarity of these names is important for my question). I do simulations
to generate ESMM, then plug these values into a second simulation function
to get ESM:
x <- list()
for (i in 1:nsimulations)
{
x$ESMM[i] <- do_simulation1()
x$ESM[i] <- do_simulation2(x$ESMM[i])
}
and I return everything as a dataframe, x <- as.data.frame(x)
When I do this, I find that x$ESMM is overwritten by x$ESM for the first
simulation. However, x$ESM is nonetheless correctly generated using
x$ESMM.
Thus, x$ESM[1] = x$ESMM[1], but for the other n-thousand simulations,
ESMM is not overwritten; the error only occurs on the first instance of
ESM.
I think I know why this is occurring: I am creating a new variable in a
list and assigning it a value, but when R cant find the variable, it
overwrites the next most similar variable (ESMM). But it still proceeds
to create the new variable ESM, having overwritten x$ESMM[1]. And it
doesnt happen for subsequent simulations, because both variables then
exist in the list.
My questions are:
1) how different do variable names have to be to avoid this problem? What
exactly is R using to decide that ESMM is the same as ESM?
or
2) is there something fundamentally flawed with the manner in which I
dynamically create variables in lists, without initializing them in some
fashion? This approach worked fine until I noticed this issue with
variables having similar names.
Thanks very much in advance for your help.
Dan Rabosky
Dan Rabosky
Department of Ecology and Evolutionary Biology
Corson Hall
Cornell University
Ithaca, NY 14853
More information about the R-help
mailing list