Get column names in R

get column names in r

This tutorial will show you how to use the names(), colnames(), and dimnames() functions to get the column names of an object in R. Do not hurry; we will start right now.

The names(), colnames(), dimnames() function

We have other articles discussing the names(), colnames(), and dimnames() functions. If you are confused, please look at the previous articles to recall your knowledge. This article will only focus on how to use functions to get column names.

Get column names in R

Use the names() function to get column names

It is quick and simple to get column names using the names() function. In this example, we will take an example showing how to use the function to get the data frame’s column names 

Code:

# Create vectors for a data frame
Name <- c("John", "David", "Mary", "Thomas", "Susan", "Emily")
Age <- c(22, 17, 41, 35, 18, 38)
Gender <- c("Male", "Male", "Female", "Male", "Female", "Female")

# Create a data frame from the vectors
df <- data.frame(Name, Age, Gender)

# Get column names of the data frame
colName <- names(df)

cat("Column names of the data frame are:\n")
cat(colName)

Result:

Column names of the data frame are:
Name Age Gender

Use the colnames() function to get column names

Another way to get the same result is using the colnames() function. The following example will show you how to do that.

Code:

# Create vectors for a data frame
Name <- c("John", "David", "Mary", "Thomas", "Susan", "Emily")
Age <- c(22, 17, 41, 35, 18, 38)
Gender <- c("Male", "Male", "Female", "Male", "Female", "Female")

# Create a data frame from the vectors
df <- data.frame(Name, Age, Gender)

# Get column names of the data frame
colName <- colnames(df)

cat("Column names of the data frame are:\n")
cat(colName)

Result:

Column names of the data frame are:
Name Age Gender

Use the dimnames() function to get column names

The dimnames() function returns a list of the object’s dimensions, including row and column names. Row names are stored in the first index of the list, and the column names are in the second index.

Code:

# Create vectors for a data frame
Name <- c("John", "David", "Mary", "Thomas", "Susan", "Emily")
Age <- c(22, 17, 41, 35, 18, 38)
Gender <- c("Male", "Male", "Female", "Male", "Female", "Female")

# Create a data frame from the vectors
df <- data.frame(Name, Age, Gender)

# Get column names of the data frame
dimName <- dimnames(df)

cat("The dimensions of the data frame are:\n")
print(dimName)

cat("The column names of the data frame are:\n")
print(dimName[2])

Result:

The dimensions of the data frame are:
[[1]]
[1] "1" "2" "3" "4" "5" "6"

[[2]]
[1] "Name" "Age" "Gender"

The column names of the data frame are:
[[1]]
[1] "Name" "Age" "Gender"

Summary

In summary, the names(), colnames(), and dimnames() functions can be used to get the column names of an object, like a data frame. We strongly recommend using the names() and colnames() to get the column names. But if you want to look at both rows and columns of the object, the dimnames() function will be more advantageous.

Maybe you are interested:

Posted in R

Leave a Reply

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