syrvn wrote on 10/13/2011 11:42:44 AM:
> 
> Hi,
> 
> 
> imagine the following matrix/data.frame
> 
> Letter Number
> a 1
> a 1
> b 1
> b 0
> c 0
> c 1
> d 0
> d 0 
> 
> If the numbers for two identical letters are also identical then I want 
to
> remove either the first or the
> second row of that letter. If for a letter the numbers are 1 and 0 I 
want to
> remove the row with the 0.
> 
> That means if the code works I would and up with the following
> matrix/data.frame
> 
> Letter Number
> a 1
> b 1
> c 1
> d 1
> 
> 
> Many thanks,
> Syrvn


I assume that you made a typo when showing the results you want. Following 
your explanation, this is what I get:

df <- data.frame(Letter=letters[rep(1:4, rep(2, 4))], Number=c(1, 1, 1, 0, 
0, 1, 0, 0))

# first, get rid of duplicates
df2 <- df[!duplicated(df), ]

# then, get rid of 0s from letters that have both 0 and 1
both01 <- tapply(df2$Number, df2$Letter, function(x) 
        any(as.integer(x)==0) & any(as.integer(x)==1))
delete <- df2$Letter %in% names(both01)[both01] & 
as.integer(df2$Number)==0
df3 <- df2[!delete, ]

# result
df3

  Letter Number
1      a      1
3      b      1
6      c      1
7      d      0


Jean
        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to