Rounding is importaint to increase the resolution for meaningful analysis and data interpretation. This inclusive tutorial includes six ways of rounding data frame in which character variables exist. Find out how to round data frame containing character variables in R.
In this tutorial, we will work on six ways of rounding data frame including the character variables in R. Firstly, we will exclude character variables and round the numeric variables. Secondly, we will use lapply() function with data.frame() function. Thirdly, we will utilize mutate_if() function available in dplyr package (Wickham et al., 2020). Then, we will round data frame containing the character variables with rapply() function. Moreover, we will round data frame by using modify_if() function available in purrr package (Henry and Wickham, 2020). Last, we will use lapply() function with sapply() function.
In this guide, we will use the part of iris dataset.
data <- head(iris, 3)
data
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
1. How to Round Data Frame by Excluding Character Variables
In this part, we exclude the character variable. Then, we round the numeric variables.
data <- head(iris, 3)
data[,-5] <-round(data[,-5], 0)
data
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5 4 1 0 setosa
## 2 5 3 1 0 setosa
## 3 5 3 1 0 setosa
Check Out: 6 Ways of Subsetting Data in R
2. How to Round Data Frame with Combinations of lapply() and data.frame() Functions
In this section, we use lapply() function in data.frame() to round data frame involving the character variable.
data <- head(iris, 3)
data.frame(lapply(data, function(x) if(is.numeric(x)) round(x, 0) else x))
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5 4 1 0 setosa
## 2 5 3 1 0 setosa
## 3 5 3 1 0 setosa
Also Check: How to Recode Character Variables in R
3. How to Round Data Frame with mutate_if() Function
We use mutate_if() function available in dplyr package (Wickham et al., 2020) to round data frame containing the character variable. We set digits argument as desired. Here, we set digits to 0.
data <- head(iris, 3)
library(dplyr)
data %>% mutate_if(is.numeric, round, digits = 0)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5 4 1 0 setosa
## 2 5 3 1 0 setosa
## 3 5 3 1 0 setosa
4. How to Round Data Frame with rapply() Function
In this part, we round the data frame involving the character variable by using rapply() function.
data <- head(iris, 3)
rapply(data, f = round, classes = "numeric", how = "replace", digits = 0)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5 4 1 0 setosa
## 2 5 3 1 0 setosa
## 3 5 3 1 0 setosa
Also Check : How to Categorize Numeric Variables in R
5. How to Round Data Frame with modify_if() Function
We use modify_if() function available in purrr R package (Henry and Wickham, 2020) to round data frame containing the character variables.
data <- head(iris, 3)
library(purrr)
modify_if(data, ~is.numeric(.), ~round(., 0))
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5 4 1 0 setosa
## 2 5 3 1 0 setosa
## 3 5 3 1 0 setosa
6. How to Round Data Frame with Combinations of lapply() and sapply() Functions
In this part, we use lapply() with sapply() functions to round data frame including the character variables.
data <- head(iris, 3)
numerics <- sapply(data, is.numeric)
data[, numerics] <- lapply(data[, numerics], round, 0)
data
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5 4 1 0 setosa
## 2 5 3 1 0 setosa
## 3 5 3 1 0 setosa
The application of the codes is available in our youtube channel below.
Don’t forget to check: How to Clean Data in R
References
Easton, T. (2017). Photo on Unsplash.
Henry, L. Wickham, H. (2020). purrr: Functional Programming Tools. R package version 0.3.4.
Wickham, H., Francois, R., Henry, L., Muller, K. (2020). dplyr: A Grammar of Data Manipulation. R package version 1.0.2.
0 Comments
1 Pingback