如何使用 R 中的列号向量提取 data.table 列?
当我们有大量的列而只有少数列可用于分析时,提取这些列就会变得有帮助。
如果我们有一个包含列号的向量,并且我们想从data.table对象中提取列,那么我们可以使用单个方括号对列进行子集设置,如下面的示例所示。
示例1
以下代码段创建data.table对象和Vector1-
x1<-rnorm(20) x2<-rnorm(20) x3<-rnorm(20) library(data.table) DT1<-data.table(x1,x2,x3) DT1
data.table创建以下内容-
x1 x2 x3 1: 0.0009381365 2.38415205 -0.02421045 2: 1.3963732982 0.16346377 1.16961063 3: 1.4794367338 -0.50184674 1.18231067 4: -1.9793913612 -0.98075371 0.44866840 5: -0.1299546054 -0.26863505 -0.19340686 6: -0.2923916937 -0.33906997 -1.77214169 7: 0.1720752818 -2.05508910 -1.05303964 8: 1.5915013897 -0.27355865 1.44198910 9: -0.2166748864 -1.37819971 0.58816401 10: 0.0715535192 0.84845987 -0.65939784 11: -0.5288076065 -0.04512962 1.27697852 12: -1.3884578057 1.95875321 -0.34228758 13: 1.2654508300 -1.69302356 0.32258719 14: -1.4042497824 -1.38131322 -0.97760370 15: 2.0428827104 -0.68046057 -1.82713480 16: -0.2539994598 -0.57296756 0.55544471 17: -0.7818319402 0.99196597 2.21323456 18: 0.7471519703 3.47030275 -1.63994775 19: -0.3480969922 1.15902926 0.89007240 20: 0.2845669502 -1.41614250 -1.69392436
为了使用Vector1提取DT1的列,将以下代码添加到上面的代码片段中-
Vector1<-c(1,3) DT1[,..Vector1]输出结果
如果您将上述所有给定的片段作为单个程序执行,它会生成以下输出-
x1 x3 1: 0.0009381365 -0.02421045 2: 1.3963732982 1.16961063 3: 1.4794367338 1.18231067 4: -1.9793913612 0.44866840 5: -0.1299546054 -0.19340686 6: -0.2923916937 -1.77214169 7: 0.1720752818 -1.05303964 8: 1.5915013897 1.44198910 9: -0.2166748864 0.58816401 10: 0.0715535192 -0.65939784 11: -0.5288076065 1.27697852 12: -1.3884578057 -0.34228758 13: 1.2654508300 0.32258719 14: -1.4042497824 -0.97760370 15: 2.0428827104 -1.82713480 16: -0.2539994598 0.55544471 17: -0.7818319402 2.21323456 18: 0.7471519703 -1.63994775 19: -0.3480969922 0.89007240 20: 0.2845669502 -1.69392436
示例2
以下代码段创建data.table对象和Vector2-
y1<-rpois(20,5) y2<-rpois(20,2) y3<-rpois(20,1) y4<-rpois(20,8) DT2<-data.table(y1,y2,y3,y4) DT2
data.table创建以下内容-
y1 y2 y3 y4 1: 5 1 1 8 2: 4 2 2 9 3: 7 4 0 6 4: 3 2 2 7 5: 5 2 1 9 6: 8 0 0 6 7: 3 0 2 9 8: 5 3 1 11 9: 5 0 1 9 10: 4 2 1 5 11: 4 2 0 7 12: 8 3 2 8 13: 1 3 4 11 14: 8 1 4 7 15: 4 3 0 10 16: 5 1 2 14 17: 7 5 0 9 18: 4 3 1 8 19: 8 3 1 9 20: 3 2 1 8
为了使用Vector2提取DT2的列,将以下代码添加到上面的代码段中-
Vector2<-c(2:4) DT2[,..Vector2]输出结果
如果您将上述所有给定的片段作为单个程序执行,它会生成以下输出-
y2 y3 y4 1: 1 1 8 2: 2 2 9 3: 4 0 6 4: 2 2 7 5: 2 1 9 6: 0 0 6 7: 0 2 9 8: 3 1 11 9: 0 1 9 10: 2 1 5 11: 2 0 7 12: 3 2 8 13: 3 4 11 14: 1 4 7 15: 3 0 10 16: 1 2 14 17: 5 0 9 18: 3 1 8 19: 3 1 9 20: 2 1 8