[R] assign to data.frame

Thomas Lumley tlumley at u.washington.edu
Thu Jun 13 22:14:14 CEST 2002


On Thu, 13 Jun 2002, Michaell Taylor wrote:
> 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?

You can't do this with assign(), which doesn't dispatch assignment
methods. In this case you can just
   d2[[V]]<-rep(NA,10)

	-thomas

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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