Skip to content

Latest commit

 

History

History
50 lines (42 loc) · 1010 Bytes

9.md

File metadata and controls

50 lines (42 loc) · 1010 Bytes

使用set()家族

set()

set()通常用来更新给定的行和列的值,要注意的是,他不能跟by结合使用。

> rows = list(3:4,5:6)
> cols = 1:2
> for (i in seq_along(rows))
+ { 
+ set(DT,
+ i=rows[[i]],
+ j = cols[i],
+ value = NA) 
+}
> DT
    V1 V2      V3 V4
 1:  1  A -0.0559  1
 2:  2  B -0.4450  2
 3: NA  C  0.0697  3
 4: NA  A -0.1547  4
 5:  1 NA -0.0559  5
 6:  2 NA -0.4450  6
 7:  1  A  0.0697  7
 8:  2  B -0.1547  8

以上程序把给定的一组行和列都设置为了NA

setname()

set()同理,setname()可以修改给定的列名和行名,以下程序是

#把名字为"old"的列,设置为"new"
> setnames(DT,"old","new") 
#把"V2","V3"列,设置为"V2.rating","V3.DataCamp"
> setnames(DT,c("V2","V3"),c("V2.rating","V3.DataCamp")) 

###setcolorder()

setcolorder()可以用来修改列的顺序。

setcolorder(DT,c("V2","V1","V4","V3")) 

这段代码会使得列的顺序变成:

[1] "V2" "V1" "V4" "V3"