[R] Get the difference of values to their own median value

Gabor Grothendieck ggrothendieck at gmail.com
Wed May 2 14:56:53 CEST 2007


Use ave.  Also its easier to use read.table to read it in and then
convert it to a matrix if that's what you want.

Input <- "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"

cn <- c("x", "y", "z")
# replace next line with this:
#  INPUT <- as.matrix(read.table("dat.dat", col.names = cn))
INPUT <- as.matrix(read.table(textConnection(Input), col.names = cn))

INPUT[,3] - ave(INPUT[,3], INPUT[,1], INPUT[,2], FUN = median)



On 5/2/07, Felix Wave <felix-wave at vr-web.de> wrote:
> Hello,
> 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.
>
> With the following line I got the median value of z for all "x" AND "y" witch
> are the same (not every measurment in my list hast the same number of
> "x" and "y" values. Sometimes lines are missing.
> >MEDIAN  <- na.omit( aggregate(INPUT[,3], by=list(INPUT[,2],INPUT[,1] ), FUN=median ) )
>
>
> To see the failin of my measurments I want to get the difference between
> every measurment and the detected median values.
> The aim would be to have a list with different colums (for every measurment)
> with the difference between measurment 1,2,3, ..., x and the median.
>
> Has anybody an idea?
>
> I though I could split the measurment list. The beginning of every measurment
> can be found with
> >START     <- grep(" 0 0.0 0.00000000", INPUT)
> But not every measurment has the same length and I don't have always the
> same number of measurments.
>
>
>
> I hope you can help me.
>
> 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" )
>
>
>
>
>
>
> #############
> ## 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
> 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