0% found this document useful (0 votes)
31 views9 pages

R Built-in Functions Guide

The document explains built-in functions in programming, categorizing them into numeric, statistical, and string functions. It provides examples for each type, detailing how to use functions like absolute, square root, mean, and string manipulation functions. These built-in functions simplify common programming tasks by allowing users to perform operations without needing to define them from scratch.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views9 pages

R Built-in Functions Guide

The document explains built-in functions in programming, categorizing them into numeric, statistical, and string functions. It provides examples for each type, detailing how to use functions like absolute, square root, mean, and string manipulation functions. These built-in functions simplify common programming tasks by allowing users to perform operations without needing to define them from scratch.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Builtin function

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))

g) lox(x) log10 Function


EXAMPLE
x<-4
print(log(4))

base long
x<-4
print(log2(4))

x<-100
print(log10(100))

8)Exponential function(e^x): This function is used to calculate the power of e


or base.
EXAMPLE
i<-5
print(i^3)

2)Statistical Function: Statistical functions are a set of tool that allow to


perform various statistical calculation. There are some statistical function
which are describe below.
a) Mean function: This function is used to find the mean value.
Example1
a<-c(0:10,40)
xm<-mean(a)
print(xm)

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)

4)Range(): It returns the value in range.


EXAMPLE:
a<-c(1:10,30)
xr<-range(a)
print(xr)
EXAMPLE-2
b<-c(2,16,17,9,12)
Print(range(b))

5)SUM(): This function is return sum.


EXAMPLE
a<-c(0:15,50)
print(sum(a))

6) Diff function: It returns difference with lag indicating which lag to use.
EXAMPLE
a<-c(0:10,40)
print(diff(a))

7)Min Function: It returns the minimum value.


EXAMPLE
b<-c(1:20,30)
print(min(b))
8)Max Function: It returns the minimum value.
EXAMPLE
b<-c(1:20,30)
print(max(b))

9)Scale function: Scale function is used to determine standardized value for


each element in a data set. It can be used to scale the values in a
vaector,matrix or [Link] scale is a technique for comparing data
that isn’t measured I the same way. The normalizing of a data set using the
mean value and standard deviation is know as scaling.
EXAMPLE
a<-matrix(1:9,3,3)
print(scale(a))

10)Factorial: Factorial is the operation of multiplying any natural number


with all the natural numbers that are smaller than it. This function is used to
find how many ways arrange or order a set number of things.
1!=1
2!=1*2
3!=1*2*3
4!=1*2*3*4

EXAMPLE
a<-c(1:10)
print(factorial(a))

3)String Function: A string is a sequence of character. There are some string


function which are describe below.
1)Substr Function: It is used to extract substrings in a character vector.
EXAMPLE
x<-"Builtinfunction"
print(substr(x,8,15))

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.
[Link]("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

7)paste Function: Paste function is used to concatenate the multiple strings. In


paste function the default separator is blank space.
EXAMPLE
y<-paste("Clinical",1,"R","Programming")
y
8)paste0: Paste0 is the faster than paste function. Paste0 remove all the space
present between the strings.
EXAMPLE
y<-paste0("Clinical",1,"R","Programming")
y

9)strsplit Function: This function is used to split the elements of character


vector.
EXAMPLE
a<-"split all the characte"
print(strsplit(a,""))
Note: If we give one space in between the quotation then the out will returns
in a separate words or [Link] If we not provide any space in between the
quotation then output returns in element wise.

Common questions

Powered by AI

The 'scale' function in R standardizes data by normalizing it via the mean and standard deviation, which allows for comparison of data that isn't initially on the same scale. This is important in statistical analysis and machine learning because it ensures that each feature contributes equally, preventing features with possibly larger variances from skewing results.

The 'grep' function in R searches for patterns within strings, providing an essential means of identifying and extracting specific sequences from data sets. By returning indices or values that match a pattern, 'grep' allows users to filter data and explore datasets based on string patterns, a critical feature for data cleaning and analysis.

The 'diff' function in R calculates the differences between successive elements of a numeric vector, representing a primary tool in analyzing variation or trends over a given set of observations. By indicating which lag to use, analysts can explore changes, making 'diff' critical for detecting shifts or trends in time-series data.

The 'substr' function extracts or replaces substrings in character vectors, allowing users to manipulate and access specific parts of strings. This is vital in data processing where precise string manipulation is required, such as extracting identifiers or modifying data labels within longer textual data.

The 'paste' function in R concatenates strings by default inserting a blank space between them, while 'paste0' concatenates without adding any spaces. Hence, 'paste' is used when separation between combined strings is needed, and 'paste0' is preferred for direct concatenation without extra spaces, providing faster performance.

One might prefer 'log10' over 'log' in R when the base 10 logarithm is more applicable, such as in scenarios involving exponential growth, orders of magnitude, or scientific fields that utilize base 10 for simplicity, like measuring sound intensity. For instance, calculating decibels, where 'log10' can simplify the representation and understanding of data.

The 'mean' function computes the average value of a numeric data set, serving as a central measure of location in statistics. It's crucial for summarizing data, identifying trends, and serving as a baseline for other statistical measures like variance and standard deviation.

The 'trunc' function in R is unique because it removes the fractional part of a number without rounding it up or down. This makes it different from other rounding functions like 'ceiling' and 'floor', which round numbers to the nearest integer based on the decimal part. Instead, 'trunc' simplifies numbers by truncating them directly, irrespective of their decimal values.

The 'ceiling' function in R rounds a decimal number up to the next highest integer, but only works in positive conditions—it does not affect negative numbers. For example, ceiling of 5.2 is 6, and -5.2 also becomes -5. 'Floor', on the other hand, rounds a decimal number down to the nearest integer and applies primarily to negative numbers, as indicated by floor of -5.2 becoming -6.

'Factorial' in R refers to the product of all positive integers up to a certain number, used to determine permutations or arrangements of distinct objects. For example, factorial(4) calculates as 4! = 4 x 3 x 2 x 1 = 24. Factorials determine combinations and permutations in statistical computations and problems involving ordering.

You might also like