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,756
  • 6
  • 18
  • 38
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,490
  • 5
  • 16
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
  • 38,442
  • 13
  • 38
  • 79
0

Example with mtcars:

sapply(mtcars, median)

SteveM
  • 1,827
  • 3
  • 11
  • 14