[R] coercing created variables into a new data frame using na.omit()

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Aug 12 14:59:31 CEST 2005

I don't know if you can read your message, but I find it exceedingly 
difficult and there seem to be several typos.  Please use the space and 
return keys ... and only send a message once.

You problem is perhaps that you are not looking at the data frame, but at 
the variable in the workspace.  attach()ing data frames is convenient but 
error-prone (as you have found).  rm(new.variable) should solve this, but 
it is better to cultivate a different style.  For example

with(data.frame1, {
# commands to create value
data.frame1$new.variable <- value
data.frame3 <- na.omit(data.frame1)

I think too that the creation of the value can be vectorized simply, 
generalizing something like

value <- (age1 - 7)*(weight - mw)

On Fri, 12 Aug 2005, sp219 wrote:

> Hi,
> I am an R newbie and one thing I am having trouble with binding variables that
> I have created within one data frame into a new data frame when using
> na.omit(). To illustrate this problem I will give the example I am working on
> and the approah I have been using:-
> data.frame1<-filepath....
> attach(data.frame1)
> #create a new variable using a function
> new.variable<-rep(1,length(weight3))
> for (x in 1:length(new.variable))
> {f<-((((age1[x]-7)*(weight[x]-mw))+((age2[x]-7)*(weight2[x]-mw))+((age3[x]-7)*
> (weight3[x]-mw)))/(((age1[x]-7)^2)+((age2[x]-7)^2)+((age3[x]-7)^2)));
> new.variable[x]<-f}
> #then bind it into the existing old data frame
> data.frame2<-cbind(data.frame1,newvariable)
> rm(dat.frame1)
> attach(data.frame2)
> #everything o.k. so far but now the problem part... I basically want to remove
> all the rows with NA in the new data frame including corresponding rows in the
> new variable
> data.frame3<-na.omit(data.frame2)
> rm(data.frame2)
> attach(data.frame3)
> length of new.variable has not changed but the length of all the other
> variables in data.frame2 has?
> Could someone please provide an explanation or an alternative route if
> possible?
> Any suggestions much appreciated,
> Thankyou, Simon Pickett
> Simon Pickett
> Centre for Ecology and Conservation Biology
> University of Exeter in Cornwall
> Tremough Campus
> Penryn
> Cornwall
> TR10 9EZ UK
> Tel: 01326371852
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

More information about the R-help mailing list