[R] Indexing by logical vectors
Christian Raschke
crasch2 at tigers.lsu.edu
Tue Jul 20 06:18:06 CEST 2010
On Mon, 2010-07-19 at 19:46 -0400, David Winsemius wrote:
> On Jul 19, 2010, at 7:16 PM, Christian Raschke wrote:
>
> > Dear R-Listers,
> >
> > My question concerns indexing vectors by logical vectors that are
> > based on the original vector. Consider the following simple example
> > to hopefully make clear what I mean:
> >
> > a <- rnorm(10)
> > a[a<0] <- NA
> >
> > However, I am now working with multiple data frames that I received,
> > where each of them has nicely descriptive, yet long names(). In my
> > scripts there are many instances where operations similar to the one
> > above are required. Again a simple example:
> >
> >
> > some.data.frame <- data.frame(some.long.variable.name=rnorm(10),
> > some.other.long.variable.name=rnorm(10))
> >
> > some.data.frame$some.other.long.variable.name[some.data.frame
> > $some.other.long.variable.name < 0] <- NA
> >
> >
> > The fact that the names are so long makes things not very readable
> > in the script and hard to debug. Is there a way in R to refer to the
> > "self" of whatever is being indexed? I am looking for something like
> >
> > some.data.frame$some.other.long.variable.name[.self < 0] <- NA
>
> There is an alternative, "is.na()<-" which I think is a bit more
> readable:
>
> is.na($some.other.long.variable.name) <- some.data.frame
> $some.other.long.variable.name < 0
Thanks, David!
As written, this throws and error. However,
is.na(some.data.frame$some.other.long.variable.name) <- some.data.frame
$some.other.long.variable.name < 0
works, but does not seem like much of an improvement to me.
>
> But do _not_ do:
>
> with(some.data.frame, is.na(some.other.long.variable.name) <-
> some.other.long.variable.name < 0 )
>
More information about the R-help
mailing list