How to find the median of all columns in an R data frame?

R ProgrammingServer Side ProgrammingProgramming

The median is the value in a vector that divide the data into two equal parts. To find the median of all columns, we can use apply function. For example, if we have a data frame df that contains numerical columns then the median for all the columns can be calculated as apply(df,2,median).

Example

Consider the below data frame −

 Live Demo

set.seed(7)
x1<-rnorm(20,5,1)
x2<-rnorm(20,100,5)
x3<-rnorm(20,100,2)
x4<-rnorm(20,25,3)
x5<-rnorm(20,30,4)
df1<-data.frame(x1,x2,x3,x4,x5)
df1

Output

      x1       x2       x3       x4       x5
1 7.287247 104.19875 102.43710 25.22911 31.37034
2 3.803228 103.52671 98.60137 25.47747 30.01699
3 4.305707 106.52982 99.42913 26.63102 30.11688
4 4.587707 93.06002  97.37689 27.11442 28.42631
5 4.029327 106.36458 99.21798 25.95691 26.82918
6 4.052720 100.92096 99.19695 28.32775 28.75319
7 5.748139 103.76140 102.70104 27.30746 28.61573
8 4.883045 102.95873 101.18238 28.46042 28.78157
9 5.152658 95.08474 100.20105 28.78205 22.85643
10 7.189978 98.61968 101.86214 27.10187 32.34910
11 5.356986 95.64574 99.47452 26.29788 36.54318
12 7.716752 103.59355 99.98466 22.23219 27.41831
13 7.281452 100.55326 100.73431 23.15325 32.47597
14 5.324021 99.60767 103.41433 22.40002 30.94557
15 6.896067 97.89755 101.44748 20.08145 33.38600
16 5.467681 97.18937 100.96207 21.02248 27.70542
17 4.106199 104.98757 96.86426 22.33289 34.47197
18 4.692672 94.47435 100.63650 23.32719 23.84000
19 4.995178 99.28856 100.33198 24.81279 28.24750
20 5.988164 101.57497 98.20018 32.26808 29.39731

Finding the median of all columns in data frame df1 −

Example

apply(df1,2,median)

Output

     x1       x2       x3          x4       x5
5.238339 100.737114 100.266517 25.717187 29.089439

Let’s have a look at another example −

Example

 Live Demo

y1<-sample(1:5,20,replace=TRUE)
y2<-sample(1:2,20,replace=TRUE)
y3<-sample(1:6,20,replace=TRUE)
y4<-sample(1:10,20,replace=TRUE)
df2<-data.frame(y1,y2,y3,y4)
df2

Output

  y1 y2 y3 y4
1  2 2  5  5
2  5 2  3  9
3  2 2  2  10
4  3 2  1  5
5  1 1  1  3
6  1 2  4  3
7  3 1  6  1
8  2 2  5  9
9  2 1  5  1
10 4 2  2  5
11 3 1  6  7
12 5 1  4  9
13 2 2  3  3
14 4 2  1  6
15 2 1  1  7
16 1 2  5  10
17 3 1  4  7
18 2 1  1  6
19 5 2  3  2
20 1 2  6  7

Finding the median of all columns in data frame df2 −

Example

apply(df2,2,median)

Output

 y1  y2  y3 y4
2.0 2.0 3.5 6.0
raja
Published on 17-Oct-2020 08:35:06
Advertisements