Modify Numbers with Comma as Thousand Separator in R (2 Examples)


This tutorial illustrates how to change thousand separators from comma to point in the R programming language.

Table of contents:

Let’s just jump right in…


Example 1: Convert Vector with Comma as Thousand Separator

In this example, I’ll explain how to modify a character vector containing numbers with comma separator to numeric data objects.

First, we have to create an example vector in R:

my_vec <- c("1234,560", "5,555", "10,101")                            # Create example vector
my_vec                                                                # Print example vector
# [1] "1234,560" "5,555"    "10,101"

You can see based on the previous output of the RStudio console that our example vector consists of three character elements. Each element is a number with comma as thousand separator.

Now, we can use the gsub and as.numeric functions to remove the commas and convert our data object to numeric:

my_vec_updated <- as.numeric(gsub(",", "", my_vec))                   # Convert example vector
my_vec_updated                                                        # Print updated example vector
# [1] 1234560    5555   10101

We have created a new vector object called my_vec_updated. This vector object contains our numbers in numeric data format.


Example 2: Convert Data Frame Columns with Comma as Thousand Separator

In the previous example I have explained how to change the format of a vector object. In this example, I’ll explain how to adjust all falsely formatted columns of a data frame.

First, we have to create some example data:

my_data <- data.frame(x1 = c("6,131", "7,835", "2,222"),              # Create example data
                      x2 = c("5,999", "3,136", "1,501"),
                      x3 = 1001:1003)
my_data                                                               # Print example data


table 1 data frame modify numbers comma as thousand separator r


Table 1 illustrates our example data frame. As you can see, the first two columns x1 and x2 contain numbers with comma punctuation mark as thousand separators. The variable x3 is already formatted the correct way.

As the next step, we have to define the columns we want to change (i.e. x1 and x2):

col_conv <- c("x1", "x2")                                             # Define variables to convert

Now, we can create a new data frame without comma separators using the lapply, as.numeric, as.character, and gsub functions:

my_data_updated <- my_data                                            # Duplicate data
my_data_updated[ , col_conv] <- lapply(my_data_updated[ , col_conv],  # Convert data
                                       function(x){ as.numeric(as.character(gsub(",", "", x))) })
my_data_updated                                                       # Print updated data


table 2 data frame modify numbers comma as thousand separator r


Table 2 shows the updated data frame that we have created with the previous R code. All commas were removed and the variable class of all columns is numeric.


Video, Further Resources & Summary

Would you like to learn more about data types and separators? Then I can recommend having a look at the following video of my YouTube channel. I illustrate the content of this tutorial in the video:


The YouTube video will be added soon.


In addition, you might read the other tutorials of this homepage:


Summary: In this tutorial you learned how to convert comma separators to numeric data objects in R. Tell me about it in the comments section, if you have further questions or comments.


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.