如何删除R中data.table对象中重复的列名?
在数据分析中,我们有时会处理重复数据或仅处理同名数据的表示。一种这样的情况是data.table对象中两列的列名相同。为此,我们可以使用 which 函数与重复函数的组合,并将该重复的输出设置为 NULL 以删除重复的列名。
示例 1
加载data.table包并创建一个data.table对象 -
library(data.table)输出结果x1<−rnorm(20)
DT1<−data.table(x1,x1)
DT1
x1 x11: −1.65034927 −1.65034927
2: −1.95441645 −1.95441645
3: 2.03530252 2.03530252
4: −2.07789754 −2.07789754
5: −1.31558491 −1.31558491
6: 0.69256432 0.69256432
7: 1.83924420 1.83924420
8: −1.59751233 −1.59751233
9: −0.12015454 −0.12015454
10: 0.46507856 0.46507856
11: 1.00867249 1.00867249
12: 1.76181383 1.76181383
13: 0.35151845 0.35151845
14: −0.29470885 −0.29470885
15: −0.01617467 −0.01617467
16: 1.28775955 1.28775955
17: −1.80266832 −1.80266832
18: −0.70682196 −0.70682196
19: −2.07815278 −2.07815278
20: 0.43574626 0.43574626
从 DT1 中删除一个 x1 -
DT1[,which(duplicated(names(DT1))):=NULL]输出结果DT1
x11: −1.65034927
2: −1.95441645
3: 2.03530252
4: −2.07789754
5: −1.31558491
6: 0.69256432
7: 1.83924420
8: −1.59751233
9: −0.12015454
10: 0.46507856
11: 1.00867249
12: 1.76181383
13: 0.35151845
14: −0.29470885
15: −0.01617467
16: 1.28775955
17: −1.80266832
18: −0.70682196
19: −2.07815278
20: 0.43574626
例2
y1<−rpois(20,5)输出结果DT2<−data.table(y1,y1)
DT2
y1 y11: 6 6
2: 5 5
3: 7 7
4: 5 5
5: 2 2
6: 3 3
7: 6 6
8: 5 5
9: 5 5
10: 3 3
11: 3 3
12: 4 4
13: 3 3
14: 6 6
15: 4 4
16: 5 5
17: 4 4
18: 3 3
19: 1 1
20: 2 2
从 DT2 中删除一个 y1 -
DT2[,which(duplicated(names(DT2))):=NULL]输出结果DT2
y11: 6
2: 5
3: 7
4: 5
5: 2
6: 3
7: 6
8: 5
9: 5
10: 3
11: 3
12: 4
13: 3
14: 6
15: 4
16: 5
17: 4
18: 3
19: 1
20: 2
以上是 如何删除R中data.table对象中重复的列名? 的全部内容, 来源链接: utcz.com/z/362109.html