R Programming Built-In Function Notes
R Programming Built-In Function Notes
2)Builtin function: The functions which are already created or defined in the
programming framework are knows as built-in functions. User doesn’t need
to create these types of functions these functions are built into an
application. End-users can access these functions by simply calling it. Built in
functions are different types which are describe below
1)Numeric Function
2)Statistical Function
3)String Function
************************************************************
1)Numeric function: R provides the various mathematical functions to
perform the mathematical calculation.
a)Absolute function():It returns the absolute value of input x, Means this
function convert negative value to positive value.
EXAMPLE
x<- -6
print(abs(x))
b)Sqaure function; This functions returns the square root of input X.
EXAMPLE
x<-125
print(sqrt(x))
c)Round function(): This function is used to do the nearest integer value
round means if the decimal value is less then .5 (eg: 4.32 – it would not
returns round value). If the decimal value is more than .5(eg: 4.72 then it
would returns round value)
EXAMPLE:
x<-c(2.8,3.25,6.72,1.23)
print(round(x))
d)Celing function: It will round to the next integer value weather the decimal
is greather than 5 or less than 5. But this function works in positive condition
only, it cannot work in negative condition means it does not change negative
value.
EXAMPLE
x<-c(5.2, -5.2)
print(ceiling(x))
e)Floor function: It will round to the same integer value or down. Means it
work on negative values only.
Example
x<-c(5.2,-5.2,2.6)
print(floor(x))
f) Truncate Function: This function removes the fractional part of the number.
Mean it remove the number of decimal place but not rounding.
EXAMPLE
y<-c(1.5,5.67,8.9765)
print(trunc(y))
base long
x<-4
print(log2(4))
x<-100
print(log10(100))
EXAMPLE2
b<-c(2,6,7,8,9)
print(mean(b))
2)Standard deviation; This function returns standard deviation of an object.
EXAMPLE-1
a<-c(0:10,40)
xm<-sd(a)
print(xm)
EXAMPLE-2
a<-c(3,9,7,2)
print(sd(a))
3)Median(): This function returns the median value.
EXAMPLE-1
a<-c(1:15,30)
xm<-median(a)
print(xm)
6) Diff function: It returns difference with lag indicating which lag to use.
EXAMPLE
a<-c(0:10,40)
print(diff(a))
EXAMPLE
a<-c(1:10)
print(factorial(a))
2)tolower: This function is used to convert the string into lower case.
EXAMPLE
x<-"TOpOne"
print(tolower(x))
3)TOUPPER: This function is used to convert the string into Uppercase case.
EXAMPLE
Y<-"topone"
print(toupper(Y))
4)str_to_title: This function is used to convert the string into Prop case. For
this function first we need to install the package(stringr) and then use this
function for propcase.
install.packages("stringr")
library(stringr)
EXAMPLE
str<-"greeks for greeks"
print(str_to_title(str))
5)grep Function: Grep function is used to search the pattern of any vector.
EXAMPLE
S2<-c("abc","ade","adef","abcdefg")
pat<-"ade"
print(grep(pat,S2))
6)Sub function: Sub function is used to replace a particular vector or string
syntax=sub(pattern,replacement,x)
EXAMPLE
a1<-"SAS PROGRAMMING"
a1
a2<-sub("SAS","R",a1)
a2