Get Column Index in Data Frame by Variable Name in R (2 Examples)
In this tutorial, I’ll illustrate how to find the index of a column in a data frame in the R programming language.
Table of contents:
You’re here for the answer, so let’s get straight to the examples…
Example Data
Consider the example data below:
data <- data.frame(x1 = 1:3, # Example data x2 = letters[1:3], x12 = 5) data # Print example data # x1 x2 x12 # 1 1 a 5 # 2 2 b 5 # 3 3 c 5
As you can see based on the previous output of the RStudio console, our example data contains three rows and the three columns x1, x2, and x12. Note that the character string “x1” (partially) matches two columns (i.e. x1 and x12).
Example 1: Extract Column Index of Variable with Exact Match
In this Section, I’ll explain how to identify variables with a complete match to a character string.
which(colnames(data) == "x1") # Apply which function # 1
The RStudio console returns the value 1, i.e. the variable with the variable name “x1” is at the first index position of our data frame.
Example 2: Extract Column Indices of Variables with Partial Match
Example 2 explains how to search for columns partially matching a character string using the grep and colnames functions:
grep("x1", colnames(data)) # Apply grep function # 1 3
The RStudio console returns the numbers 1 and 3, i.e. the column names at the first and third index position contain the character pattern “x1”.
Video, Further Resources & Summary
Do you need more explanations on the R programming codes of this article? Then you may have a look at the following video of my YouTube channel. In the video, I illustrate the R programming code of this tutorial.
The YouTube video will be added soon.
In addition, you may want to read the related tutorials of my homepage:
- Select Data Frame Column Using Character Vector
- Return Column Name of Largest Value for Each Row
- Find Index of Element in Vector in R
- Find Index of Maximum & Minimum Value of Vector & Data Frame Row
- which Function in R
- The R Programming Language
At this point you should have learned how to identify the variable index based on its label in a data frame in the R programming language. Please let me know in the comments section, in case you have additional questions.
Statistics Globe Newsletter