In this post, I am going to cover the common built-in numeric and basic statistical functions. All of the functions are coming with the base package so you don't need to add or install any packages to use them.
How to get absolute value of a number?
mynumber <- -90.91 abs(mynumber)
mynumbers <- c(-9, 0, 5, -4.5) abs(mynumbers)
[1] 90.91
[1] 9.0 0.0 5.0 4.5
How to calculate square root?
How to get nearest integer?
mynumber <- 12.34 #this will give you nearest integer up ceiling(mynumber) #this will give you nearest integer down floor(mynumber)
mynumbers <- c(2.3, 4.6, 1.5) ceiling(mynumbers) floor(mynumbers)
[1] 13
[1] 12
[1] 3 5 2
[1] 2 4 1
How to round an integer?
How to find median value? (Middle of sorted list of numbers)
How to find the range of array?
Basic summary and finding minimum/maximum value of an array.
mynumbers <- c(2,5,10,20,50) sum(mynumbers) min(mynumbers) max(mynumbers)
[1] 87
[1] 2
[1] 50
How to generate sequence of numbers?
How to repeat the same value multiple times?
How to sort array?
mynumbers <- c(34,89,23,45,10,29,73,49,1,55,83,99) # This will give you the sorted index. order(mynumbers) # This will give you sorted value mynumbers[order(mynumbers)] # This will sort by desc mynumbers[order(-mynumbers)]
[1] 1 10 23 29 34 45 49 55 73 83 89 99
[1] 99 89 83 73 55 49 45 34 29 23 10 1

No comments:
Post a Comment