[R] finding average of entire rows to equal values in a vector

arun smartpink111 at yahoo.com
Sat Jun 7 10:39:10 CEST 2014


Hi,
This is not clear.

If this is a combination of rows using a specific formula as you showed, use ?combn
dat <- read.table(text="0.7, 0.3, 0.6, 0.9
1.0, 0.1, 0.4, 0.7
1.2, 0.8, 0.3, 0.1",sep=",",header=FALSE) 

indx <- combn(seq(dim(dat)[1]), 2)
vec1 <- c(0.86, 0.46, 0.5, 0.63)
indx1 <- sapply(seq_len(ncol(indx)), function(i) {
    ind <- indx[, i]
    val <- unlist(round((2 * dat[ind[1], ] + dat[ind[2], ])/3, 2))
    tol <- 0.015
    all(abs(val - vec1) <= tol)
})
dat[indx[, indx1], ]
#   V1  V2  V3  V4
#1 0.7 0.3 0.6 0.9
#3 1.2 0.8 0.3 0.1


A.K.  



Hi,

I'm looking for a way to find the average of several rows, of a 4 x 7 matrix, that will eventually reach a fixed set of values in a vector while always averaging the entire row from the 4x7 to get there. So... in a matrix of 4 columns and 7 rows with values like

0.7, 0.3, 0.6, 0.9
1.0, 0.1, 0.4, 0.7
1.2, 0.8, 0.3, 0.1
...
..
.

how can i write it to find the possible combinations of rows 1, 2, and 3 so that it matches the vector: 0.86, 0.46, 0.5, 0.63 with some small margin of error?

in this case the answer is (2x(row 1)+(row 3))/3

best,
-sunny0 




More information about the R-help mailing list