- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to extract data.table columns using a vector of column numbers in R?
When we have large number of columns and only few of them are useful for analysis then extraction of such columns becomes helpful.
If we have a vector that contains column numbers and we want to extract the columns from a data.table object then we can use the single square brackets for subsetting of columns as shown in the below given examples.
Example 1
Following snippet creates data.table object and Vector1 −
x1<-rnorm(20) x2<-rnorm(20) x3<-rnorm(20) library(data.table) DT1<-data.table(x1,x2,x3) DT1
The following data.table is created −
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
In order to extract columns of DT1 using Vector1, add the following code to the above snippet −
Vector1<-c(1,3) DT1[,..Vector1]
Output
If you execute all the above given snippets as a single program, it generates the following output −
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
Example 2
Following snippet creates data.table object and 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
The following data.table is created −
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
In order to extract columns of DT2 using Vector2, add the following code to the above snippet −
Vector2<-c(2:4) DT2[,..Vector2]
Output
If you execute all the above given snippets as a single program, it generates the following output −
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
To Continue Learning Please Login
Login with Google