# Extract data.table Column as Vector Using Index Position in R (Example)

This tutorial illustrates how to convert a data.table variable to a vector in the R programming language.

Letâ€™s jump right to the exampleâ€¦

## Example Data & Add-On Packages

First, we have to install and load the data.table package, if we want to use the functions that are contained in the package:

```install.packages("data.table")  # Install data.table package

Now, we can use the data.table function to create an exemplifying table in R:

```data <- data.table(x1 = 1:5,    # Data
x2 = letters[1:5],
x3 = 3)
data                            # Print data
#    x1 x2 x3
# 1:  1  a  3
# 2:  2  b  3
# 3:  3  c  3
# 4:  4  d  3
# 5:  5  e  3```

As you can see based on the previous output of the RStudio console, our example data contains of five rows and three variables.

## Example: Convert data.table Variable to Vector

In this Section, Iâ€™ll illustrate how to use a column of our data.table as vector (or array). In order to extract a variable as vector, we simply need to subset our data using two square brackets (as we would when extracting data from a list). Have a look at the following R code:

```vec_x2 <- data[[2]]             # Extract by index position
vec_x2                          # Print vector
# "a" "b" "c" "d" "e"```

Our vector contains the characters that are also contained in the variable x2 in our example data.

## Video & Further Resources

Do you want to know more about the handling of data.tables? Then you might want to have a look at the following video of my YouTube channel. In the video, Iâ€™m explaining the R code of the present tutorial.

In addition to the video, you may read the related tutorials on my homepage.

In summary: In this R tutorial you learned how to modify data.table columns. Please tell me about it in the comments section below, if you have additional questions. Furthermore, donâ€™t forget to subscribe to my email newsletter in order to get updates on the newest articles.

Subscribe to the Statistics Globe Newsletter