# How to find the row variance of columns having same name in data.table object in R?

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

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

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

## Example

#### Create the data.table object

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

library(data.table)
DT<-
data.table(x=rpois(25,5),y=rpois(25,5),x=rpois(25,2),y=rpois(25,1),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:   2 11 1 1
2:   4  5 2 1
3:   5  4 2 1
4:   5  5 0 0
5:   5  1 0 1
6:   8  6 3 2
7:   4  4 4 1
8:   4 12 4 2
9:   4  4 2 0
10:  8  6 1 3
11:  9 10 1 0
12:  5  3 1 0
13:  7  4 0 0
14:  6  4 4 0
15:  8  7 4 1
16: 10  3 1 0
17:  3 10 3 1
18:  3  4 3 2
19:  8  3 2 3
20:  4  4 0 1
21:  5  2 2 3
22: 10  5 2 2
23:  7  8 2 0
24:  7  7 3 0
25:  7  7 2 0
x  y x y

Find the row variance of columns having same name

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

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

## Output

        x   y
[1,]   0.5 50.0
[2,]   2.0  8.0
[3,]   4.5  4.5
[4,]  12.5 12.5
[5,]  12.5  0.0
[6,]  12.5  8.0
[7,]   0.0  4.5
[8,]   0.0 50.0
[9,]   2.0  8.0
[10,] 24.5  4.5
[11,] 32.0 50.0
[12,]  8.0  4.5
[13,] 24.5  8.0
[14,]  2.0  8.0
[15,]  8.0 18.0
[16,] 40.5  4.5
[17,]  0.0 40.5
[18,]  0.0  2.0
[19,] 18.0  0.0
[20,]  8.0  4.5
[21,]  4.5  0.5
[22,] 32.0  4.5
[23,] 12.5 32.0
[24,]  8.0 24.5
[25,] 12.5 24.5

Updated on: 15-Nov-2021

46 Views

##### Kickstart Your Career

Get certified by completing the course

Advertisements