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

Felix Wave felix-wave at vr-web.de
Thu Apr 19 15:19:19 CEST 2007


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           <- readLines(dat.dat)
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"



More information about the R-help mailing list