How to fill a data.table row with missing values in R?


Instead of filling missing values, we sometimes need to replace the data with missing values. This might be required in situations when missing values are coded with a number or the actual values are not useful or sensible for the data study. Also, we might want to replace the values with something else in the future.

Check out the below given examples to understand how we can fill data.table row with missing values.

Example 1

Following snippet creates a data.table object −

library(data.table)
x1<-rnorm(20)
x2<-rnorm(20)
x3<-rnorm(20)
DT1<-data.table(x1,x2,x3)
DT1

The following data.table object is created −

        x1           x2           x3
1:   0.359330986   0.20756943  -1.41681109
2:   3.030837814  -0.90236706   0.18453973
3:   0.094325185  -0.62989777  -0.76818543
4:  -0.605302247   0.57308532  -0.26006696
5:   0.589317918  -0.48431919   0.29462134
6:   0.924100008  -1.43570087  -1.68280918
7:  -1.211456354   0.86749925   1.39952788
8:   0.061726522   1.10284992  -0.61436950
9:  -0.981008050   0.18694454   2.67624706
10: -1.386289393   1.60893091   2.05774337
11: -0.978221580  -0.20008714  -1.03519166
12: -1.553948892  -1.36348786  -0.37988549
13: -1.002634550  -1.00437648   0.44634500
14:  0.280758507  -0.76264247  -0.36987504
15: -0.002528128   1.68233987   0.16512468
16:  1.200738477  -0.13188273  -0.19674097
17:  1.062584867   0.66075529  -0.06017969
18: -0.956870759   0.92754861   0.91910574
19: -1.323343765  -2.20655283   1.18144943
20: -1.618360372  -0.02947935  -0.53886698

In order to fill the first row in DT1 with missing values, add the following code to the above snippet −

DT1[1,(names(DT1)):=.SD[NA]]
DT1

Output

If you execute all the above given snippets as a single program, it generates the following output: −

          x1        x2            x3
1:       NA         NA            NA
2:   3.030837814  -0.90236706   0.18453973
3:   0.094325185  -0.62989777  -0.76818543
4:  -0.605302247   0.57308532  -0.26006696
5:   0.589317918  -0.48431919   0.29462134
6:   0.924100008  -1.43570087  -1.68280918
7:  -1.211456354   0.86749925   1.39952788
8:   0.061726522   1.10284992  -0.61436950
9:  -0.981008050   0.18694454   2.67624706
10: -1.386289393   1.60893091   2.05774337
11: -0.978221580  -0.20008714  -1.03519166
12: -1.553948892  -1.36348786  -0.37988549
13: -1.002634550  -1.00437648   0.44634500
14:  0.280758507  -0.76264247  -0.36987504
15: -0.002528128   1.68233987   0.16512468
16:  1.200738477  -0.13188273  -0.19674097
17:  1.062584867   0.66075529  -0.06017969
18: -0.956870759   0.92754861   0.91910574
19: -1.323343765  -2.20655283   1.18144943
20: -1.618360372  -0.02947935  -0.53886698

Example 2

Following snippet creates a data.table object −

y1<-rpois(20,1)
y2<-rpois(20,5)
y3<-rpois(20,1)
y4<-rpois(20,2)
DT2<-data.table(y1,y2,y3,y4)
DT2

The following data.table object is created −

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

In order to fill the fifth row in DT2 with missing values, add the following code to the above snippet −

DT2[5,(names(DT2)):=.SD[NA]]
DT2

Output

If you execute all the above given snippets as a single program, it generates the following output: −

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

Updated on: 10-Nov-2021

358 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements