# How to find the row median of columns having same name in R data frame?

R ProgrammingServer Side ProgrammingProgramming

#### Artificial Intelligence : The Future Of Programming

15 Lectures 54 mins

#### Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

#### C Programming from scratch- Master C Programming

Best Seller

60 Lectures 8 hours

To find the row median of columns having same name in R data frame, we can follow the below steps −

• First of all, create a data frame with some columns having same name.

• Then, use tapply along with colnames and median function to find the row median of columns having same name.

## Example

#### Create the data frame

Let’s create a data frame as shown below −

df<-
data.frame(x=rpois(25,1),x=rpois(25,10),x=rpois(25,5),y=rpois(25,3),y=rpois(25,5),y=rp ois(25,5),check.names=FALSE)
df

## Output

On executing, the above script generates the below output(this output will vary on your system due to randomization) −

   x  x x y y y
1  0 10 5 5 3 4
2  0  9 3 3 3 2
3  2  6 2 2 6 1
4  0 19 3 2 5 5
5  4 11 7 4 7 5
6  0 11 3 7 4 4
7  1  4 4 8 7 5
8  0  7 4 4 4 6
9  0 15 2 4 2 6
10 0  4 4 3 6 4
11 1 16 8 1 4 4
12 2 11 5 1 5 1
13 1 18 2 2 6 3
14 1 11 3 4 5 4
15 0 11 5 6 2 6
16 1  7 3 1 2 4
17 1 11 4 4 5 5
18 0  8 3 1 5 2
19 0  9 6 3 5 5
20 1  7 5 1 4 3
21 2 14 7 5 3 6
22 2 11 6 4 7 9
23 1 16 5 4 5 3
24 0  5 4 4 2 4
25 1  9 4 6 2 3

Find the row median of columns having same name

Using tapply along with colnames and median function to find the row median of columns having same name in data frame df −

df<-
data.frame(x=rpois(25,1),x=rpois(25,10),x=rpois(25,5),y=rpois(25,3),y=rpois(25,5),y=rp ois(25,5),check.names=FALSE)
t(apply(df,1, function(x) tapply(x,colnames(df),median)))

## Output

      x y
[1,]  5 4
[2,]  3 3
[3,]  2 2
[4,]  3 5
[5,]  7 5
[6,]  3 4
[7,]  4 7
[8,]  4 4
[9,]  2 4
[10,] 4 4
[11,] 8 4
[12,] 5 1
[13,] 2 3
[14,] 3 4
[15,] 5 6
[16,] 3 2
[17,] 4 5
[18,] 3 2
[19,] 6 5
[20,] 5 3
[21,] 7 5
[22,] 6 7
[23,] 5 4
[24,] 4 4
[25,] 4 3
Updated on 09-Nov-2021 05:41:24