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

To find the row standard deviation 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 sd function to find the row standard deviation 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,1),x=rpois(25,4),y=rpois(25,10),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:   3 2 2  6
2:   6 1 4  8
3:   6 1 2  8
4:   3 0 2 10
5:   2 1 1 14
6:   5 1 8  7
7:   3 2 6 10
8:   5 0 1  7
9:   5 0 4 10
10:  5 1 5 12
11:  6 1 3 13
12:  2 1 6 18
13:  1 0 6 10
14:  7 1 5 3
15: 11 0 3 15
16:  3 1 6 10
17:  4 0 3  2
18:  5 3 3  8
19:  8 2 4 15
20:  5 1 1 11
21: 10 0 4 10
22:  4 1 6  7
23:  7 0 6 10
24:  5 1 6  9
25:  6 1 5  5
x y x  y

Find the row standard deviation of columns having same name

Using tapply along with colnames and sd 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,1),x=rpois(25,4),y=rpois(25,10),check.names=FALSE)
t(apply(DT,1, function(x) tapply(x,colnames(DT),sd)))

## Output

         x         y
[1,]  0.7071068 2.828427
[2,]  1.4142136 4.949747
[3,]  2.8284271 4.949747
[4,]  0.7071068 7.071068
[5,]  0.7071068 9.192388
[6,]  2.1213203 4.242641
[7,]  2.1213203 5.656854
[8,]  2.8284271 4.949747
[9,]  0.7071068 7.071068
[10,] 0.0000000 7.778175
[11,] 2.1213203 8.485281
[12,] 2.8284271 12.020815
[13,] 3.5355339 7.071068
[14,] 1.4142136 1.414214
[15,] 5.6568542 10.606602
[16,] 2.1213203 6.363961
[17,] 0.7071068 1.414214
[18,] 1.4142136 3.535534
[19,] 2.8284271 9.192388
[20,] 2.8284271 7.071068
[21,] 4.2426407 7.071068
[22,] 1.4142136 4.242641
[23,] 0.7071068 7.071068
[24,] 0.7071068 5.656854
[25,] 0.7071068 2.828427