How to Round Off Numbers in R: A Comprehensive Guide

The R language is capable of performing from easy to advanced numerical calculations. Although R can compute any computation up to 16 digits accurately, a user may not always want to use (or get) that too many digits in his final results or computations. In such cases, one can use a couple of functions to round off numbers in R Language. To round off a number to two or more digits after the decimal point, one can use the round() function as follows:

Round off Numbers in R Language

round(123.456,digits = 2)

##
123.46

One can also use the round() function to round off numbers to multiples of 10, 100, and so on. For that purpose, one just needs to add a negative number as the digits argument: For example

round(-123.456,digits = -2)

##
-100
Round of Numbers in R Language

Significant Digits in R Language

If someone needs to specify the number of significant digits to be retained, regardless of the size of the number, you use the signif() function instead:

signif(-123.456,digits = 4)
##
-123.5

signif(-123.456, digits=3)
##
-123

signif(-123.456, digits=2)
##
-120

Both round() and signif() round off the numbers to the nearest possible number. So, if the first digit that is dropped is smaller than 5, the number is rounded down. If the number is bigger than 5, the number is rounded up. On the other hand, if the first digit that is dropped is exactly 5, R Language uses a rule that is common in programming languages: Always round to the nearest even number. For example, round(1.5) and round(2.5) both return 2, Similarly, for example, round(-4.5) returns -4.

Rounding off Numbers floor(), ceiling(), and trunc() Functions

Contrary to round(), three other functions always round off the numbers in the same direction:

floor(x) rounds to the nearest integer that is smaller than $x$. So, floor(123.45) becomes 123 and floor(-123.45) becomes –124.

ceiling(x) rounds to the nearest integer that’s larger than $x$. This means ceiling(123.45) becomes 124 and ceiling(-123.45) becomes –123.

trunc(x) rounds to the nearest integer in the direction of 0. So, trunc(123.65) becomes 123 and trunc(-123.65) becomes –123.

https://itfeature.com

https://gmstat.com

Numeric Data Type in R Language

The article is about Numeric Data Type in R Language. Decimal values are referred to as numeric data types in R, which is the default working out data type for numbers in R Language.

Numeric Data Type in R Language

Assigning a decimal value to a variable $x$ creates a variable that has a numeric data type. For example

x <- 6.2
print(x)

Since numeric data types consist of numbers, one can perform different mathematical operations such as addition, subtraction, multiplication, division, etc.

Class of Numeric Data Type

In R, the class of numeric variables is numeric. One can check the class of a numeric object ($x$) by using class() function.

class(x)
Numeric Data Type in R

Converting Character Type to Numeric Type in R

In R Language, the as.numeric() function is used to convert a vector of character values to a numeric value. Note that by default, R converts character vectors to factors.

One can confirm the data type of an object by using a function is.numeric(). For example,

is.numeric(x)

If is.numeric(x) results in an output of TRUE then it means that the data type of the variable/object $x$ is numeric. Let’s assign a whole number to a variable $y$ and then check the class of object $y$:

y <- 2
class(y)
[1] "numeric"

It means that the default data type for numbers is the numeric type in R Language. One can also use typeof() function to confirm the data type of a variable.

Creating Numeric Vectors

One can also create a variable (called a numeric vector) by using the numeric function in R. It will create a vector of zeros. For example,

z <- numeric(5)
print(z)

[1] 0 0 0 0 0

class(z)

[1] "numeric"


Other methods also exist for the creation of numeric vectors. Note that the numeric data type is different from the integer.

MCQs Data Basic Statistics Quiz

In Summary, the numeric data type in R is a fundamental data structure for numerical computations in R. Understanding its properties and when to potentially use the integer data type is essential for effective data analysis in R.

Frequently Asked Questions About R Numeric Data type in R

Best Statistical Inference Quiz in R 14

The article contains a Statistical Inference quiz in R language with Answers. There are 16 questions in the “Statistical Inference Quiz in R Language”. The MCQs are from probability and regression models. Let us Start with the Statistical Inference Quiz in R.

Statistical Inference Quiz in R Language

1. Consider the following data set. What is the intercept for fitting the model with $x$ as the predictor and $y$ as the outcome?
x <- c(0.8, 0.47, 0.51, 0.73, 0.36, 0.58, 0.57, 0.85, 0.44, 0.42)
y <- c(1.39, 0.72, 1.55, 0.48, 1.19, -1.59, 1.23, -0.65, 1.49, 0.05)

 
 
 
 

2. Consider the mtcars data set. Fit a model with mpg as the outcome that considers numbers of cylinders as a factor variable and weight as a confounder. Now fit a second model with mpg as the outcome model that considers the interaction between numbers of cylinders (as a factor variable) and weight. Give the P-value for the likelihood ratio test comparing the two models and suggest a model using 0.05 as a type I error rate significance benchmark.

 
 
 
 

3. The number of people showing up at a bus stop is assumed to be Poisson with a mean of 5 people per hour. You watch the bus stop for 3 hours. About what’s the probability of viewing 10 or fewer people?

 
 
 
 

4. Consider the following data set

x <- c(0.586, 0.166, -0.042, -0.614, 11.72)
y <- c(0.549, -0.026, -0.127, -0.751, 1.344)

Give the slope dfbeta for the point with the highest hat value.

influence.measures(fit5)$infmat[which.max(abs(influence.measures(fit5)$infmat[, 2])), 2]

 
 
 
 

5. You flip a fair coin 5 times, about what’s the probability of getting 4 or 5 heads?

 
 
 
 

6. Consider a standard uniform density. The mean for this density is 0.5 and the variance is 1 / 12. You sample 1,000 observations from this distribution and take the sample mean, what value would you expect it to be near?

 
 
 
 

7. Consider the mtcars data set. Fit a model with mpg as the outcome that includes numbers of cylinders as a factor variable and weight as a possible confounding variable. Compare the effect of 8 versus 4 cylinders on mpg for the adjusted and unadjusted by-weight models. Here, adjusted means including the weight variable as a term in the regression model and unadjusted means the model without weight included. What can be said about the effect comparing 8 and 4 cylinders after looking at models with and without weight included?

 
 
 
 

8. Brain volume for adult women is normally distributed with a mean of about 1,100 cc for women with a standard deviation of 75 cc. What brain volume represents the 95th percentile?

 
 
 
 

9. Consider the mtcars data set. Fit a model with mpg as the outcome that includes a number of cylinders as a factor variable and weight as a confounder. Give the adjusted estimate for the expected change in mpg comparing 8 cylinders to 4.

 
 
 
 

10. Consider the mtcars data set. Fit a model with mpg as the outcome that includes the number of cylinders as a factor variable and weight included in the model as

lm(mpg ~ I(wt * 0.5) + factor(cyl), data = mtcars)

How is the wt coefficient interpreted?

 
 
 
 

11. Consider the following data set
x <- c(0.586, 0.166, -0.042, -0.614, 11.72)
y <- c(0.549, -0.026, -0.127, -0.751, 1.344)
Give the hat diagonal for the most influential point

 
 
 
 

12. Consider the following PMF shown below in R
x <- 1:4
p <- x/sum(x)
temp <- rbind(x, p)
rownames(temp) <- c("X", "Prob")
temp
What is the mean?

 
 
 
 

13. Consider the following data set
x <- c(0.8, 0.47, 0.51, 0.73, 0.36, 0.58, 0.57, 0.85, 0.44, 0.42)
y <- c(1.39, 0.72, 1.55, 0.48, 1.19, -1.59, 1.23, -0.65, 1.49, 0.05)
Fit the regression through the origin and get the slope treating $y$ as the outcome and $x$ as the regressor.

(Hint, do not center the data since we want regression through the origin, not through the means of the data.)

 
 
 
 

14. Suppose that diastolic blood pressures (DBPs) for men aged 35-44 are normally distributed with a mean of 80 (mm Hg) and a standard deviation of 10. About what is the probability that a random 35-44-year-old has a DBP less than 70?

 
 
 
 

15. Do data(mtcars) from the datasets package and fit the regression model with mpg as the outcome and weight as the predictor. Give the slope coefficient.

 
 
 
 

16. The respiratory disturbance index (RDI), a measure of sleep disturbance, for a specific population has a mean of 15 (sleep events per hour) and a standard deviation of 10. They are not normally distributed. Give your best estimate of the probability that a sample average RDI of 100 people is between 14 and 16 events per hour.

 
 
 
 

Statistical Inference Quiz in R Language

Statistical Inference Quiz in R with Answers

  • Consider the following PMF shown below in R
    x <- 1:4 p <- x/sum(x)
    temp <- rbind(x, p)
    rownames(temp) <- c(“X”, “Prob”)
    temp
    What is the mean?
  • Suppose that diastolic blood pressures (DBPs) for men aged 35-44 are normally distributed with a mean of 80 (mm Hg) and a standard deviation of 10. About what is the probability that a random 35-44-year-old has a DBP less than 70?
  • Brain volume for adult women is normally distributed with a mean of about 1,100 cc for women with a standard deviation of 75 cc. What brain volume represents the 95th percentile?
  • You flip a fair coin 5 times, about what’s the probability of getting 4 or 5 heads?
  • The respiratory disturbance index (RDI), a measure of sleep disturbance, for a specific population has a mean of 15 (sleep events per hour) and a standard deviation of 10. They are not normally distributed. Give your best estimate of the probability that a sample average RDI of 100 people is between 14 and 16 events per hour.
  • Consider a standard uniform density. The mean for this density is 0.5 and the variance is 1 / 12. You sample 1,000 observations from this distribution and take the sample mean, what value would you expect it to be near?
  • The number of people showing up at a bus stop is assumed to be Poisson with a mean of 5 people per hour. You watch the bus stop for 3 hours. About what’s the probability of viewing 10 or fewer people?
  • Consider the mtcars data set. Fit a model with mpg as the outcome that includes a number of cylinders as a factor variable and weight as a confounder. Give the adjusted estimate for the expected change in mpg comparing 8 cylinders to 4.
  • Consider the mtcars data set. Fit a model with mpg as the outcome that includes the number of cylinders as a factor variable and weight included in the model as
    lm(mpg ~ I(wt * 0.5) + factor(cyl), data = mtcars)
    How is the wt coefficient interpreted?
  • Consider the following data set
    x <- c(0.586, 0.166, -0.042, -0.614, 11.72)
    y <- c(0.549, -0.026, -0.127, -0.751, 1.344)
    Give the hat diagonal for the most influential point
  • Consider the following data set
    x <- c(0.586, 0.166, -0.042, -0.614, 11.72)
    y <- c(0.549, -0.026, -0.127, -0.751, 1.344)
    Give the slope dfbeta for the point with the highest hat value. influence.measures(fit5)$infmat[which.max(abs(influence.measures(fit5)$infmat[, 2])), 2]
  • Consider the mtcars data set. Fit a model with mpg as the outcome that includes a number of cylinders as a factor variable and weight as a possible confounding variable. Compare the effect of 8 versus 4 cylinders on mpg for the adjusted and unadjusted by-weight models. Here, adjusted means including the weight variable as a term in the regression model and unadjusted means the model without weight included. What can be said about the effect comparing 8 and 4 cylinders after looking at models with and without weight included?
  • Consider the mtcars data set. Fit a model with mpg as the outcome that considers a number of cylinders as a factor variable and weight as a confounder. Now fit a second model with mpg as the outcome model that considers the interaction between numbers of cylinders (as a factor variable) and weight. Give the P-value for the likelihood ratio test comparing the two models and suggest a model using 0.05 as a type I error rate significance benchmark.
  • Consider the following data set
    x <- c(0.8, 0.47, 0.51, 0.73, 0.36, 0.58, 0.57, 0.85, 0.44, 0.42)
    y <- c(1.39, 0.72, 1.55, 0.48, 1.19, -1.59, 1.23, -0.65, 1.49, 0.05)
    Fit the regression through the origin and get the slope treating $y$ as the outcome and $x$ as the regressor. (Hint, do not center the data since we want regression through the origin, not through the means of the data.)
  • Do data(mtcars) from the datasets package and fit the regression model with mpg as the outcome and weight as the predictor. Give the slope coefficient.
  • Consider the following data set. What is the intercept for fitting the model with $x$ as the predictor and $y$ as the outcome?
    x <- c(0.8, 0.47, 0.51, 0.73, 0.36, 0.58, 0.57, 0.85, 0.44, 0.42)
    y <- c(1.39, 0.72, 1.55, 0.48, 1.19, -1.59, 1.23, -0.65, 1.49, 0.05)

Sampling and Sampling Distributions Quiz with Answers

Intermediate Mathematics Part-I Quiz with Answers