简体   繁体   中英

how do you find the median of 2 columns using R?

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?

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

Another solution:

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

which has the advantage of returning a data frame.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM