tags:

views:

406

answers:

2

Hi ,

Im trying to compute the median vector of a data set s with column A1 and B1 , The median vector is the median for each observation from both columns.

I tried to do this and it didnt work .

median(s[c("A1","B1")])

Is there another way to do it ?

+3  A: 

The median of two observations is simply the mean. So rowMeans(s[,c("A1","B1")]). Equivalently, apply(s[,c("A1","B1")],1,median)

Rob Hyndman
yes it works ! I was thinking of a doulbe loop already ,this is much simpler.r = 0 for (i in 1:2512){ for (j in c(1,5,9,13,17,21,25,29,33)) { result = median(s[i,j],s[i,j+4],s[i,j+8],s[i,j+12]) r = rbind(r,result) }}
Rbuilder
+2  A: 

Another solution:

library(plyr)
colwise(median)(s[c("A1", "B1")])

which has the advantage of returning a data frame.

hadley
somehow my R doesnt recognize this command
Rbuilder
You need to install the plyr package, first.
William Doane