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

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Aug 12 21:21:57 CEST 2005


On Fri, 12 Aug 2005, Kjetil Brinchmann Halvorsen wrote:

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

No, it's just a sketch of a style.

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

I intended that the changed object be returned: so for example

test <- with(test, {test$c <- 1:5; test})

does work.  What I really meant to write (and had tested) could be 
sketched as

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

but cut-and-paste got two lines out of order.

> 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

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