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

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Sat 13 Aug 2005 - 05:21:57 EST

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

______________________________________________
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 06:35:05 2005

This archive was generated by hypermail 2.1.8 : Sun 23 Oct 2005 - 15:17:38 EST