r-squared Slide 1 www.r-squared.in/rprogramming R Programming Learn the fundamentals of data analysis with R.
r-squared Slide 2 Course Modules www.r-squared.in/rprogramming ✓ Introduction ✓ Elementary Programming ✓ Working With Data ✓ Selection Statements ✓ Loops ✓ Functions ✓ Debugging ✓ Unit Testing
r-squared Slide 3 Working With Data www.r-squared.in/rprogramming ✓ Data Types ✓ Data Structures ✓ Data Creation ✓ Data Info ✓ Data Subsetting ✓ Comparing R Objects ✓ Importing Data ✓ Exporting Data ✓ Data Transformation ✓ Numeric Functions ✓ String Functions ✓ Mathematical Functions
r-squared In this unit, we will explore the following numeric functions: Slide 4 Numeric Functions www.r-squared.in/rprogramming ● signif() ● jitter() ● format() ● formatC() ● abs() ● round() ● ceiling() ● floor()
r-squared Slide 5 abs() www.r-squared.in/rprogramming Description abs() computes the absolute values of its arguments. Syntax abs(x) Returns Absolute value Documentation help(abs)
r-squared Slide 6 abs() www.r-squared.in/rprogramming Examples > # example 1 > x <- -5 > abs(x) [1] 5 > # example 2 > y <- 5 > abs(y) [1] 5 > # example 3 > z <- c(1, -3, 4, -7, 5, -9) > abs(z) [1] 1 3 4 7 5 9
r-squared Slide 7 round() www.r-squared.in/rprogramming Description round() rounds its argument to the specified number of decimal places. Syntax round(x, digits) Returns Argument rounded to specified number of decimal places. Documentation help(round)
r-squared Slide 8 round() www.r-squared.in/rprogramming Examples > # example 1 > x <- 5.3645 > round(x) # zero decimal values [1] 5 > round(x, digits = 1) # one decimal values [1] 5.4 > round(x, digits = 2) # two decimal values [1] 5.36 > round(x, digits = 3) # three decimal values [1] 5.364
r-squared Slide 9 ceiling() www.r-squared.in/rprogramming Description ceiling() takes a numeric argument x and returns the smallest integer not less than x. Syntax ceiling(x) Returns Integer Documentation help(ceiling)
r-squared Slide 10 ceiling() www.r-squared.in/rprogramming Examples > example 1 > x <- 5.3645 > ceiling(x) [1] 6 > example 2 > x <- 3.94 > ceiling(x) [1] 4 > example 3 > x [1] 7.012865 8.148132 9.840098 2.965393 2.098276 6.139226 3.819461 8.849482 1.068249 [10] 5.105874 > ceiling(x) [1] 8 9 10 3 3 7 4 9 2 6
r-squared Slide 11 floor() www.r-squared.in/rprogramming Description floor() takes a numeric argument x and returns the smallest integer not greater than x. Syntax floor(..., collapse = NULL) Returns Integer Documentation help(floor)
r-squared Slide 12 floor() www.r-squared.in/rprogramming Examples > # example 1 > x <- 5.3645 > floor(x) [1] 5 > # example 2 > x <- 3.94 > floor(x) [1] 3 > # example 3 > x <- sample(jitter(1:10)) > x [1] 6.1581438 9.9260513 0.9823364 4.1083687 4.9102557 8.1316709 7.0094556 2.8870083 [9] 2.1403249 9.0941759 > floor(x) [1] 6 9 0 4 4 8 7 2 2 9
r-squared Slide 13 trunc() www.r-squared.in/rprogramming Description trunc() takes a numeric argument and returns the first integer as the values is truncated towards zero. Syntax trunc(x) Returns Integer Documentation help(trunc)
r-squared Slide 14 trunc() www.r-squared.in/rprogramming Examples > # example 1 > x <- 5.3645 > trunc(x) [1] 5 # as we truncate the value in x towards zero, the first integer that appears is 5. > # example 2 > x <- -3.94 > trunc(x) [1] -3 > round(x) [1] -4 > floor(x) [1] -4 # as we truncate the value in x towards zero, the first integer that appears is -3.
r-squared Slide 15 signif() www.r-squared.in/rprogramming Description signif() rounds the value in the first argument to the specified number of significant digits. Syntax signif(x, digits) Returns Value with specified number of significant digits Documentation help(signif)
r-squared Slide 16 signif() www.r-squared.in/rprogramming Examples > # example > x <- 5.3645 > signif(x, 1) [1] 5 > signif(x, 2) [1] 5.4 > signif(x, 3) [1] 5.36
r-squared Slide 17 jitter() www.r-squared.in/rprogramming Description jitter() add noise to a numeric vector. Syntax jitter(numeric_vector) Returns Numeric vector with noise Documentation help(jitter)
r-squared Slide 18 jitter() www.r-squared.in/rprogramming Examples > # example 1 > x <- 1:10 > x [1] 1 2 3 4 5 6 7 8 9 10 > jitter(x) [1] 1.198246 1.845626 3.171562 3.809923 5.188604 6.171728 7.022194 8.058092 [9] 9.150582 10.142704
r-squared Slide 19 format() www.r-squared.in/rprogramming Description format() will format an R object for pretty printing. Syntax format(x, digits, nsmall, justify) Returns Formatted object Documentation help(format)
r-squared Slide 20 format() www.r-squared.in/rprogramming Examples > # example 1 > x [1] 1.187272 2.080868 3.197517 4.016246 4.979482 6.163807 6.837692 8.013903 8.864735 [10] 9.939144 > format(x, digits = 3) [1] "1.19" "2.08" "3.20" "4.02" "4.98" "6.16" "6.84" "8.01" "8.86" "9.94" > # example 2 > x <- 1:10 > format(x) [1] " 1" " 2" " 3" " 4" " 5" " 6" " 7" " 8" " 9" "10" > format(x, trim = TRUE) [1] "1" "2" "3" "4" "5" "6" "7" "8" "9" "10"
r-squared Slide 21 format() www.r-squared.in/rprogramming Examples > # example 3 > format(6.5) [1] "6.5" > format(6.5, nsmall = 3) [1] "6.500" > format(c(6.5, 15.3), digits = 2) [1] " 6.5" "15.3" > format(c(6.5, 15.3), digits = 2, nsmall = 1) [1] " 6.5" "15.3"
r-squared Slide 22 formatC() www.r-squared.in/rprogramming Description formatC() formats numbers individually and flexibly. Syntax formatC(x, digits, width) Returns Formatted object Documentation help(formatC)
r-squared Slide 23 formatC() www.r-squared.in/rprogramming Examples > # example 1 > x <- 1:10 > formatC(x) [1] "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" > formatC(x, width = 6) [1] " 1" " 2" " 3" " 4" " 5" " 6" " 7" " 8" " 9" [10] " 10" > # example 2 > x <- sample(jitter(1:10)) > x [1] 7.0094486 0.9592379 5.8403164 8.8848952 4.9665959 9.9507841 3.1295332 7.8283830 [9] 2.1360850 3.8991551 > formatC(x, digits = 4) [1] "7.009" "0.9592" " 5.84" "8.885" "4.967" "9.951" " 3.13" "7.828" "2.136" [10] "3.899"
r-squared In the next unit, we will explore string manipulation in R using the following functions: Slide 24 Next Steps... www.r-squared.in/rprogramming ● match() ● char.expand() ● grep() ● grepl() ● sub() ● substr() ● substring() ● strsplit() ● strtrim() ● chartr() ● tolower() ● toupper() ● toString() ● nchar() ● nzchar() ● noquote() ● pmatch() ● charmatch()
r-squared Slide 25 Connect With Us www.r-squared.in/rprogramming Visit r-squared for tutorials on: ● R Programming ● Business Analytics ● Data Visualization ● Web Applications ● Package Development ● Git & GitHub

R Programming: Numeric Functions In R