[R] Subset based on multiple values

arun smartpink111 at yahoo.com
Thu Jul 12 08:46:26 CEST 2012


Hi,

Try this: (a variant of andrija)
 testct<-table(test)
 subset(test,!is.na(match(test,as.integer(names(testct[testct%in%max(testct)])))))

    [,1]
[1,]    1
[2,]    1
[3,]    1
[4,]    1
[5,]    7
[6,]    7
[7,]    7
[8,]    7

A.K.



----- Original Message -----
From: Amanduh320 <aadams26 at uwo.ca>
To: r-help at r-project.org
Cc: 
Sent: Wednesday, July 11, 2012 2:33 PM
Subject: [R] Subset based on multiple values

I'm stuck on a seemingly simple problem. I'm trying to subset the data by
several numbers and it cuts out half of the rows. Here is the sample code:

test <- as.matrix(c(1,1,1,1,3,3,7,7,7,7))
Count <- tapply(test[,1], test[,1], length)   # count for each value
spp <- unique(test[,1])
Count1 <- as.data.frame(cbind(Count,spp))
Max <- max(Count)
Count1$sppMax <- ifelse(Count1$Count >= Max, Count1$spp, 0)  # only keep
values that =Max
Count2 <- subset(Count1, sppMax > 0)    #get rid of values that are less
than Max
AllMax <- unique(Count2$sppMax)
test2 <- subset(test, test[,1] == AllMax)


this works where there is only one value for AllMax, how to make it work
when there are multiple?

Thank you!

--
View this message in context: http://r.789695.n4.nabble.com/Subset-based-on-multiple-values-tp4636159.html
Sent from the R help mailing list archive at Nabble.com.

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




More information about the R-help mailing list