How to extract a data.table row as a vector in R?

R ProgrammingServer Side ProgrammingProgramming

A data.table object is similar to a data frame object but there are few things that can be specifically applied to a data.table because data.table package functions was defined for a data.table object only. If we want to extract a data.table row as a vector then we can use as.vector function along with as.matrix so that the as.vector can read the row properly.

Loading data.table package:

> library(data.table)

Consider the below vectors and create a data.table object:

Example

> x1<-sample(LETTERS[1:3],20,replace=TRUE)
> x2<-sample(LETTERS[1:4],20,replace=TRUE)
> x3<-sample(LETTERS[1:5],20,replace=TRUE)
> x4<-sample(LETTERS[2:5],20,replace=TRUE)
> x5<-sample(LETTERS[3:5],20,replace=TRUE)
> DT1<-data.table(x1,x2,x3,x4,x5)
> DT1

Output

x1 x2 x3 x4 x5
1: B C C D E
2: B C D B E
3: B C C B E
4: C D A C E
5: C C D E D
6: A D D E E
7: A C A D E
8: B B E D E
9: C D B D C
10: A B E E E
11: C A D B C
12: C D A B E
13: C A E C C
14: A A A B C
15: C C E D D
16: B B C D D
17: A A D D E
18: B C D C E
19: C B D D E
20: C B C C D

Extracting rows as vectors:

Example

> Row1<-as.vector(as.matrix(DT1[1]))
> Row1
[1] "B" "C" "C" "D" "E"
> is.vector(Row1)
[1] TRUE

> Row2<-as.vector(as.matrix(DT1[2]))
> Row2
[1] "B" "C" "D" "B" "E"
> is.vector(Row2)
[1] TRUE

> Row5<-as.vector(as.matrix(DT1[5]))
> Row5
[1] "C" "C" "D" "E" "D"
> is.vector(Row5)
[1] TRUE

> Row10<-as.vector(as.matrix(DT1[10]))
> Row10
[1] "A" "B" "E" "E" "E"
> is.vector(Row10)
[1] TRUE
> is.vector(Row10)
[1] TRUE

> Row18<-as.vector(as.matrix(DT1[18]))
> Row18
[1] "B" "C" "D" "C" "E"
> is.vector(Row18)
[1] TRUE

Let’s have a look at another example:

Example

> v1<-rnorm(20,1,2.3)
> v2<-rnorm(20,1,0.5)
> v3<-rnorm(20,1,0.275)
> v4<-rnorm(20,1,1.05)
> v5<-rnorm(20,1,0.80)
> v6<-rnorm(20,1,0.007)
> DT2<-data.table(v1,v2,v3,v4,v5)
> DT2

Output

v1 v2 v3 v4 v5
1: 3.0343715 1.50341176 0.7697336 1.1133711891 -0.6121658
2: -0.9852696 1.67132579 0.6735064 -0.4264159308 2.0648977
3: -0.1772886 0.92605020 1.6350086 -0.1306917763 0.2362759
4: -1.5987827 1.21532168 0.8452163 1.1970737419 1.3881985
5: 6.6638926 0.99153886 0.9312445 0.6709141801 0.1946506
6: -1.1456574 1.17003856 0.9168460 0.2278830477 0.2643338
7: 0.9558536 0.66266842 0.8268585 -0.0526142041 1.8055215
8: 0.3400104 1.26874416 0.5101432 -0.0008413192 1.1666347
9: 1.0587648 -0.02557012 1.2874365 2.6673114134 1.3532956
10: -1.4587479 2.04442403 0.6002114 0.6081632615 3.0083355
11: 5.5237850 0.89874276 1.3020908 2.0082148902 1.3465480
12: 2.2144533 1.13244217 1.0143068 -0.7514250499 1.4106594
13: -0.4355997 0.99994424 1.3501380 2.2319042291 0.9450547
14: 1.2946799 0.26674188 0.7165047 0.5596915326 0.2925462
15: 0.2576993 0.68885846 1.4244761 2.2760543161 -0.4573567
16: 1.2538944 1.82497873 0.6419318 0.0770117052 1.7090877
17: -1.3401457 0.58804734 1.1919157 2.9846478595 0.2938903
18: 4.6633082 0.50940850 1.5545408 1.5738985335 0.3506627
19: -2.0482789 0.82047093 1.1950368 -0.9818883889 0.6690500
20: -4.4615852 1.23548986 0.6505639 1.1960534288 2.3088988

Example

> Row1<-as.vector(as.matrix(DT2[1]))
> Row1
[1] 3.0343715 1.5034118 0.7697336 1.1133712 -0.6121658
> is.vector(Row1)
[1] TRUE

> Row12<-as.vector(as.matrix(DT2[12]))
> Row12
[1] 2.214453 1.132442 1.014307 -0.751425 1.410659
> is.vector(Row12)
[1] TRUE

> Row20<-as.vector(as.matrix(DT2[20]))
> Row20
[1] -4.4615852 1.2354899 0.6505639 1.1960534 2.3088988
> is.vector(Row20)
[1] TRUE

> Row15<-as.vector(as.matrix(DT2[15]))
> Row15
[1] 0.2576993 0.6888585 1.4244761 2.2760543 -0.4573567
> is.vector(Row15)
[1] TRUE

> Row19<-as.vector(as.matrix(DT2[19]))
> Row19
[1] -2.0482789 0.8204709 1.1950368 -0.9818884 0.6690500
> is.vector(Row19)
[1] TRUE
raja
Published on 07-Nov-2020 12:18:56
Advertisements