[R] subsetting dataframe by rownames to be excluded

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Oct 13 15:01:49 CEST 2008


On Mon, 13 Oct 2008, Dieter Menne wrote:

> Prof Brian Ripley <ripley <at> stats.ox.ac.uk> writes:
>
>> Yes: DF[is.na(match(row.names(DF), exclude_me)), ]
>
> Assuming everything is possible in R: would it be possible to make the below
> work without breaking existing code?

It would be possible, but not I think desirable.

c(exclude) is fine (works now, does nothing useful except strip 
attributes).  But -<char vector> will give an error: that's not 
necessarily the end, as `[` is a SPECIALSXP and so is passed unevaluated 
arguments.  However, its first step is method dispatch and that evaluates 
all the arguments, so a substantial internal rewrite would be needed.

It would be fairly easy to make

subset(a, subset=-exclude)

work, and select=-<col_name> already works.  I think though that messing 
with `[` would be too dangerous, and would also lead to expectations that 
all its methods should accept this notation (and hence many would need to 
be re-written, including [.data.frame as used here).  And then people 
would expect this to work on RHS, so [<- would need to be re-written ....

>
>
> a <- data.frame(x=1:10)
> rownames(a) = letters[1:10]
> exclude = c("a","c")
> a[is.na(match(row.names(a), exclude)), ] # not really that easy to remember
>
> a[-c(1,3),]
> # In analogy....
> a[-c(exclude),]   #invalid argument to unary operator
>
>
> Dieter
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

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