如何用R中的缺失值填充data.table行?
我们有时需要用缺失值替换数据,而不是填充缺失值。在缺失值用数字编码或实际值对数据研究无用或不合理的情况下,可能需要这样做。此外,我们可能希望将来用其他东西替换这些值。
查看下面给出的示例以了解我们如何data.table用缺失值填充行。
示例 1
以下代码段创建了一个data.table对象 -
library(data.table)x1<-rnorm(20)
x2<-rnorm(20)
x3<-rnorm(20)
DT1<-data.table(x1,x2,x3)
DT1
data.table创建以下对象 -
x1 x2 x31: 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
为了用缺失值填充 DT1 中的第一行,将以下代码添加到上面的代码片段中 -
DT1[1,(names(DT1)):=.SD[NA]]输出结果DT1
如果您将上述所有给定的代码片段作为单个程序执行,它会生成以下输出: -
x1 x2 x31: 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
示例 2
以下代码段创建了一个data.table对象 -
y1<-rpois(20,1)y2<-rpois(20,5)
y3<-rpois(20,1)
y4<-rpois(20,2)
DT2<-data.table(y1,y2,y3,y4)
DT2
data.table创建以下对象 -
y1 y2 y3 y41: 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
为了用缺失值填充 DT2 中的第五行,将以下代码添加到上面的代码段中 -
DT2[5,(names(DT2)):=.SD[NA]]输出结果DT2
如果您将上述所有给定的代码片段作为单个程序执行,它会生成以下输出: -
y1 y2 y3 y41: 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
以上是 如何用R中的缺失值填充data.table行? 的全部内容, 来源链接: utcz.com/z/327402.html