R Convert Variable of data.table to Vector by Its Index Position (Example Code)

In this tutorial you’ll learn how to convert a data.table variable to a vector in R programming.

Setting up the Example

install.packages("data.table")          # Install & load data.table package
library("data.table")
my_tab <- data.table(x = c(1, 7, 4),    # Our example data
                   y = c(5, 4, 9),
                   z = 3:1)
my_tab                                  # Returning data.table to console
#    x y z
# 1: 1 5 3
# 2: 7 4 2
# 3: 4 9 1

Example: Extracting data.table column as Vector

tab_vec <- my_tab[[3]]                  # Converting to vector
tab_vec                                 # Printing vector
# 3 2 1

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top