4

I am 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 did not work.

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

Is there another way to do it?

4

2 回答 2

7

两个观察值的中位数就是平均值。所以rowMeans(s[,c("A1","B1")])。等效地,apply(s[,c("A1","B1")],1,median)

于 2009-10-31T03:42:16.807 回答
7

另一种解决方案:

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

它具有返回数据帧的优点。

于 2009-10-31T04:13:43.457 回答