[R] subset data.frame with value != in all columns
Peter Dalgaard
p.dalgaard at biostat.ku.dk
Thu Feb 3 22:21:43 CET 2005
"Tim Howard" <tghoward at gw.dec.state.ny.us> writes:
> I am trying to extract rows from a data.frame based on the
> presence/absence of a single value in any column. I've figured out how
> to do get the positive matches, but the remainder (rows without this
> value) eludes me. Mining the help pages and archives brought me,
> frustratingly, very close, as you'll see below.
>
> My goal: two data frames, one with -99 in at least one column in each
> row, one with no occurrences of -99. I want to preserve rownames in
> each.
>
> My questions:
> Is there a cleaner way to extract all rows containing a specified
> value?
> How can I extract all rows that don't have this value in any col?
>
> #create dummy dataset
> x <- data.frame(
> c1=c(-99,-99,-99,4:10),
> c2=1:10,
> c3=c(1:3,-99,5:10),
> c4=c(10:1),
> c5=c(1:9,-99))
> subset(x,apply(x==-99,1,any))
c1 c2 c3 c4 c5
1 -99 1 1 10 1
2 -99 2 2 9 2
3 -99 3 3 8 3
4 4 4 -99 7 4
10 10 10 10 1 -99
> subset(x,!apply(x==-99,1,any))
c1 c2 c3 c4 c5
5 5 5 5 6 5
6 6 6 6 5 6
7 7 7 7 4 7
8 8 8 8 3 8
9 9 9 9 2 9
--
O__ ---- Peter Dalgaard Blegdamsvej 3
c/ /'_ --- Dept. of Biostatistics 2200 Cph. N
(*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
More information about the R-help
mailing list