How to Round Values in Specific Columns Using dplyr


You can use the following methods to round values in specific columns of a data frame using the dplyr package in R:

Method 1: Round Values in Specific Columns

library(dplyr)

#round values in 'sales' and 'returns' columns to 2 decimal places 
df_new <- df %>% mutate(across(c('sales', 'returns'), round, 2))

Method 2: Round Values in All Numeric Columns

library(dplyr)

#round values in all numeric columns to 2 decimal places
df_new <- df %>% mutate(across(where(is.numeric), round, 2))

The following examples show how to use each method in practice with the following data frame in R:

#create data frame
df <- data.frame(store=c('A', 'A', 'A', 'B', 'B', 'C', 'C', 'C'),
                 sales=c(4.352, 6.5543, 7.5423, 9.22111, 4.332, 9.55, 8.0094, 7.2),
                 returns=c(1.2324, 2.6654, 3.442, 6.545, 8.11, 8.004, 7.545, 6.0),
                 promos=c(12.11, 14.455, 10.277, 23.51, 20.099, 29.343, 30.1, 45.6))

#view data frame
df

  store   sales returns promos
1     A 4.35200  1.2324 12.110
2     A 6.55430  2.6654 14.455
3     A 7.54230  3.4420 10.277
4     B 9.22111  6.5450 23.510
5     B 4.33200  8.1100 20.099
6     C 9.55000  8.0040 29.343
7     C 8.00940  7.5450 30.100
8     C 7.20000  6.0000 45.600

Example 1: Round Values in Specific Columns Using dplyr

The following code shows how to round the values in the sales and returns columns to 2 decimal places:

library(dplyr)

#round values in 'sales' and 'returns' columns to 2 decimal places 
df_new <- df %>% mutate(across(c('sales', 'returns'), round, 2))

#view updated data frame
df_new

  store sales returns promos
1     A  4.35    1.23 12.110
2     A  6.55    2.67 14.455
3     A  7.54    3.44 10.277
4     B  9.22    6.54 23.510
5     B  4.33    8.11 20.099
6     C  9.55    8.00 29.343
7     C  8.01    7.54 30.100
8     C  7.20    6.00 45.600

Notice that the values in the sales and returns columns are rounded to 2 decimal places while all other columns have remain unchanged.

Example 2: Round Values in All Numeric Columns Using dplyr

The following code shows how to round the values in all of the numeric columns to 2 decimal places:

library(dplyr)

#round values in all numeric columns 2 decimal places 
df_new <- df %>% mutate(across(where(is.numeric), round, 2))

#view updated data frame
df_new

  store sales returns promos
1     A  4.35    1.23  12.11
2     A  6.55    2.67  14.46
3     A  7.54    3.44  10.28
4     B  9.22    6.54  23.51
5     B  4.33    8.11  20.10
6     C  9.55    8.00  29.34
7     C  8.01    7.54  30.10
8     C  7.20    6.00  45.60

Notice that the values in all three numeric columns of the data frame have been rounded to 2 decimal places.

Related: How to Use the across() Function in dplyr

Additional Resources

The following tutorials explain how to perform other common tasks in dplyr:

dplyr: How to Mutate Variable if Column Contains String
dplyr: How to Change Factor Levels Using mutate()
dplyr: How to Sum Across Multiple Columns

Leave a Reply

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