# [R] Using "mean" if two values are identical

Stephen Tucker brown_emu at yahoo.com
Thu Apr 19 17:27:22 CEST 2007

```## making data up
# make matrix with some equal values
> mat <- cbind(x=rnorm(10),y=rnorm(10),z=rnorm(10))
> mat[c(8,9),"y"] <- mat[c(1,7),"x"]
> mat
x          y           z
[1,]  0.26116849  0.5823529 -0.96924020
[2,] -0.21415406  0.1085396  2.00542549
[3,]  0.56890081 -1.2526322  0.08539552
[4,] -1.09096693 -1.9369088  0.03079587
[5,] -1.31749886 -1.1437411 -0.29125624
[6,] -0.45941172  0.2997472  0.10329381
[7,]  0.39586456 -0.2587432 -1.29788184
[8,] -0.05066363  0.2611685 -0.47942195
[9,] -0.87602919  0.3958646 -0.53205231
[10,]  0.30059621 -1.9531231  0.22398194
>

## find the index of y which corresponds to equivalent value of
## x and find mean. the following function will give search
## through for each x the matching values of y and return
## the value of x, the index of y, and the mean value
> t(apply(mat[,c("x","z")], MARGIN=1, FUN=function(v,w) {
+   yindex <- which(abs(v["x"]-w[,"y"]) < .Machine\$double.eps^0.5)
+   if(length(yindex) > 0) {
+     c(xVal=v["x"],indexOfy=yindex,meanVal=mean(c(v["z"],w[yindex,"z"])))
+   } else {
+     c(xVal=v["x"],indexOfy=NA,meanVal=NA)
+   }
+ },w=mat[,c("y","z")]))
x indexOfy   meanVal
[1,]  0.26116849        8 -0.724331
[2,] -0.21415406       NA        NA
[3,]  0.56890081       NA        NA
[4,] -1.09096693       NA        NA
[5,] -1.31749886       NA        NA
[6,] -0.45941172       NA        NA
[7,]  0.39586456        9 -0.914967
[8,] -0.05066363       NA        NA
[9,] -0.87602919       NA        NA
[10,]  0.30059621       NA        NA

Hope this helps.

--- Felix Wave <felix-wave at vr-web.de> wrote:

> Hello,
> I have got a question.
> I've got a matrix (mail end) with the colnames x, y, z. In this matrix
> are different measurements. x and y are risign coordinates.
>
> My question. Always, if the "x" AND "y" coordinates are the same, I want to
>
> get the mean of their z values.
>
>
> e.q. "
> x" AND "y" in line1 and line8 are identical:
> 29 4.5 --> mean of 1.505713 and 1.495148
>
>
> Thank's a lot.
> Felix
>
>
>
>
> ###############
> ## My R Code ##
> ###############
> INPUT           <- gsub("^ ", "", INPUT)
> INPUT           <- t( sapply( strsplit(INPUT, split=" "), as.numeric ) )
> colnames(INPUT) <- c("x", "y", "z" )
>
>
> # HERE START's my PROBLEM #
> if (duplicated(INPUT[,1] & INPUT[,2] ))
>   zMEAN   <- mean(INPUT[,3] )
>
> # MATRIX with the mean-z values #
> zMATRIX <- matrix(c(INPUT[,1], INPUT[,2], INPUT[,3] ), ncol=3, byrow=FALSE)
>
>
>
>
> #############
> ## dat.dat ##
> #############
> 29 4.5 1.505713
> 29 4.6 1.580402
> 29 4.7 1.656875
> 29 4.8 1.735054
> 30 0 0
> 30 0.1 0.00096108
> 30 0.2 0.00323831
> 29 4.5 1.495148
> 29 4.6 1.568961
> 29 4.7 1.644467
> 30 0 0
> 30 0.1 0.00093699
> 30 0.2 0.00319411
> 30 0.3 0.00676619"
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help