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

I am trying to calculate the medians of a dataset vector swith a column A1and 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 any other way to do this?

+3
source share
2 answers

The median of two observations is just the average. So rowMeans(s[,c("A1","B1")]). Equivalentlyapply(s[,c("A1","B1")],1,median)

+6
source

Another solution:

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

which has the advantage of returning a data frame.

+6
source

Source: https://habr.com/ru/post/1721559/


All Articles