0

How could I find the median value of a column in R. Each of my column has almost three to five values in it. The result should only be one value for each column.

SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41
Shary
  • 1
  • 2
    Refer to [this question](https://stackoverflow.com/questions/21644848/summarizing-multiple-columns-with-dplyr), and use the `median` function – IceCreamToucan Oct 23 '20 at 15:16

3 Answers3

0
foo <- c(1,2,3)
median(foo)

And in a data frame:

df <- data.frame(
    a = c(1,2,3)
  )
median(df[,"a"])
MKR
  • 1,620
  • 7
  • 20
0

With dplyr and using iris dataframe for example:

library(dplyr)
#Data
data("iris")
#Code
iris %>% select(-Species) %>% summarise_all(median,na.rm=T)

Output:

  Sepal.Length Sepal.Width Petal.Length Petal.Width
1          5.8           3         4.35         1.3
Duck
  • 39,058
  • 13
  • 42
  • 84
0

Example with mtcars:

sapply(mtcars, median)

SteveM
  • 2,226
  • 3
  • 12
  • 16