[R] subsetting dataframe by rownames to be excluded

David Winsemius dwinsemius at comcast.net
Mon Oct 13 21:00:20 CEST 2008


On Oct 13, 2008, at 5:36 AM, 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?
>
>
> 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

Given the negative to your question, I wonder if you would find, as I  
hope works for me, that it will be easier to remember this  
(equivalent) form?

 > a[ ! row.names(a) %in% exclude, ]
[1]  2  4  5  6  7  8  9 10

... equivalent because, per the help page, %in% is defined by  
function(x,table) {match( x, table , nomatch=0) > 0}  and the nomatch  
argument "converts" the NA's properly from a logical perspective. The  
help page defines a %w/o% function in just such a manner.

-- 
David Winsemius
Heritage Labs



More information about the R-help mailing list