[R] select rows by criteria
Rui Barradas
rui1174 at sapo.pt
Thu Mar 1 16:18:37 CET 2012
Hello,
syrvn wrote
>
> Hello,
>
> I am stuck with selecting the right rows from a data frame. I think the
> problem is rather how to select them
> then how to implement the R code.
>
> Consider the following data frame:
>
> df <- data.frame(ID = c(1,2,3,4,5,6,7,8,9,10), value =
> c(34,12,23,25,34,42,48,29,30,27))
>
> What I want to achieve is to select 7 rows (values) so that the mean value
> of those rows are closest
> to the value of 35 and the remaining 3 rows (values) are closest to 45.
> However, each value is only
> allowed to be sampled once!
>
> Any ideas, how to achieve that?
>
>
> Cheers
>
See
?combn
It gives all possible combinations as a matrix (default) or list. Then,
'apply'.
#---------------------------
# Name changed to 'DF',
# 'df' is the R function for the F distribution density
# (and a frequent choice for example data in R-help!)
#
DF <- data.frame(ID = c(1,2,3,4,5,6,7,8,9,10),
value = c(34,12,23,25,34,42,48,29,30,27))
f <- function(j, v, const) abs(mean(v[j]) - const)
inxmat <- with(DF, combn(ID, 7))
meansDist1 <- apply(inxmat, 2, function(jnx) f(jnx, DF$value, 35))
(i1 <- which(meansDist1 == min(meansDist1)))
inxmat <- with(DF, combn(ID, 3))
meansDist2 <- apply(inxmat, 2, function(jnx) f(jnx, DF$value, 45))
(i2 <- which(meansDist2 == min(meansDist2)))
meansDist3 <- meansDist1 + meansDist2 # Compromise of both criteria?
(i3 <- which(meansDist3 == min(meansDist3)))
Maybe it's combn(1:10, 3)[, 101] you want, or maybe there's another way to
compromise the two criteria.
Hope this helps,
Rui Barradas
--
View this message in context: http://r.789695.n4.nabble.com/select-rows-by-criteria-tp4434812p4435257.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list