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

From: Kjetil Brinchmann Halvorsen <kjetil_at_acelerate.com>
Date: Sat 13 Aug 2005 - 03:05:07 EST

Prof Brian Ripley wrote:

>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)
>
>
>
That cannot possible work, as assignment within with is local to with's environment. I have used superassigmnent for this (<<-), but that cannot possible
be a good style?

Look at the following:

 > test <- data.frame( a=1:5, b=1:5)
 > test
  a b
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
 > with(test, test$c <- 1:5)
 > test
  a b
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
 > with(test, test$c <<- 1:5)
 > test
  a b c
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
5 5 5 5

So what is the best style her?

Kjetil

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

-- 

Kjetil Halvorsen.

Peace is the most effective weapon of mass construction.
               --  Mahdi Elmandjra





-- 
Internal Virus Database is out-of-date.
Checked by AVG Anti-Virus.

______________________________________________
R-help@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
Received on Sat Aug 13 03:33:30 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:39:47 EST