X. Prefix in Column Names when Reading Data Frame in R (Example)

 

In this article you’ll learn how to modify the X. prefix when importing data in R programming.

Table of contents:

Let’s dive right into the examples.

 

Example Data

Let’s assume that we want to read a CSV file that looks as shown in the screenshot below:

 

import csv file

 

As you can see, our CSV file has five rows and four columns. Note that the first three columns have numeric column names.

 

Example 1: Import CSV File with X. Column Name Prefix

This example illustrates why R assigns X. in front of the column names when reading a data frame into R.

Let’s assume that we want to read our CSV file using the read.csv2 function:

my_data <- read.csv2("my_data.csv")                  # Import data
my_data                                              # Print imported data

 

table 1 data frame x prefix column names when reading data frame r

 

As you can see based on Table 1, our imported data frame contains the five rows and four columns of our CSV file.

However, the R programming language has added the prefix X or X. before the first three variable names during the process of import.

The reason for this is that the column names of these columns contained numbers and special characters at the beginning of the column name.

The first two column names started with a number and hence the R programming language added the prefix X.

The third column name started with a – sign and hence the R programming language added the prefix X. (i.e. X and a point).

The fourth column name was kept as in the CSV file, since it started with a valid character.

So far so good, but how could we remove the X prefix from our variable names? That’s what I’m going to show next.

 

Example 2: Modify Column Names of Imported Data Frame

Example 2 illustrates how to rename the column names of our data frame using the colnames function in R.

Have a look at the following R code and the updated data frame shown in Table 2:

colnames(my_data) <- paste0("Col", 1:ncol(my_data))  # Change column names
my_data                                              # Print updated data

 

table 2 data frame x prefix column names when reading data frame r

 

By running the previously shown R programming code we have created Table 2, i.e. a data frame with manually specified column names.

 

Video, Further Resources & Summary

If you need further explanations on the R codes of this tutorial, you might have a look at the following video of my YouTube channel. I explain the R programming codes of this article in the video.

 

The YouTube video will be added soon.

 

In addition, you could read the related tutorials of this website.

 

Summary: You have learned in this article how to change the X. prefix when reading a data frame in the R programming language. Let me know in the comments section below, in case you have additional questions.

 

Subscribe to my free statistics newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


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.

Menu
Top