How to find the row means in data.table object for columns having same name in R?

R ProgrammingServer Side ProgrammingProgramming

To find the row mean of columns having same name in R data.table, we can follow the below steps −

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

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

Example

Create the data.table

Let’s create a data.table as shown below −

library(data.table)
DT<-
data.table(x=rpois(25,2),y=rpois(25,1),x=rpois(25,10),y=rpois(25,5),check.names=FALSE)
DT

Output

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

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

Find the row mean of columns having same name

Using tapply along with colnames and mean function to find the row mean of columns having same name in data.table DT −

library(data.table)
DT<-
data.table(x=rpois(25,2),y=rpois(25,1),x=rpois(25,10),y=rpois(25,5),check.names=FALSE)
t(apply(DT,1, function(x) tapply(x,colnames(DT),mean)))

Output

        x   y
[1,]   7.5 2.0
[2,]   3.5 2.5
[3,]   5.0 2.0
[4,]   4.5 2.0
[5,]   5.0 2.0
[6,]   6.0 2.5
[7,]   5.5 3.5
[8,]   4.5 1.5
[9,]   4.0 2.5
[10,]  7.0 2.0
[11,]  5.0 3.5
[12,]  5.5 2.0
[13,]  4.5 2.0
[14,]  7.0 2.0
[15,]  3.5 4.0
[16,]  6.0 3.0
[17,]  2.0 3.5
[18,]  5.0 2.0
[19,]  5.5 4.0
[20,]  5.0 2.5
[21,]  6.0 3.5
[22,]  4.5 4.5
[23,]  7.0 3.5
[24,]  7.5 0.5
[25,] 11.0 4.5
raja
Published on 15-Nov-2021 07:07:12
Advertisements