[R] assign to data.frame
Michaell Taylor
pols1oh at bestweb.net
Thu Jun 13 21:49:45 CEST 2002
I have two large data.frames which I would like to combine. Merge works, but
is slow and a "merge" is not really required. Rather a rbind will do the
trick. However, there are a number of variables which need to be created in
each data.frame to make the columns of the two data.frames similar.
say,
d1 <- data.frame(ind1=1:10,ind2=letters[1:10],c_rnorm(10))
d2 <- data.frame(ind1=21:30,ind2=letters[11:20],e_rnorm(10))
thus, d1 contains 'c' and d2 contains 'e' - with no overlap in ind1 and ind2.
If I :
d1$e <- rep(NA,10)
d2$c <- rep(NA,10)
I can,
d <- rbind(d1,d2) # which is where I want to go.
However, there are actually a number of such variables to be created. Sooo, I
tried:
add.to.d2 <- names(d1)[!is.element(names(d1),names(d2)]
for (V in add.to.d2)
assign(paste('d2$',V,sep=''),rep(NA,10))
}
(ditto for the other data.frame)
which gives me a d2$c in the global space, but not in the d2 dataframe. I see
options for environment specification in assign, but can't seem to manage to
specify a dataframe as the environ. Also tried: assign(as.name(paste('d2$',V,
sep='')),rep(NA,10)) -- which wouldn't seem needed, and indeed, not.
gotta be something I am missing and I am sure it is in the help files
somewhere, but .... any hints?
Thanks.
Michaell
=========================================
Michaell Taylor, PhD
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list