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

sp219 S.Pickett at exeter.ac.uk
Fri Aug 12 14:41:54 CEST 2005


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




More information about the R-help mailing list