0% found this document useful (0 votes)
4 views4 pages

R Built-In Functions R Numeric Functions: Abs (X)

The document provides an overview of built-in functions in R, including numeric functions like abs, sqrt, and exp, as well as string manipulation functions such as substr, grep, and sub. It also discusses user-defined functions and demonstrates how to work with strings in vectors, matrices, arrays, data frames, and lists. Examples are provided for each function to illustrate their usage.
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)
4 views4 pages

R Built-In Functions R Numeric Functions: Abs (X)

The document provides an overview of built-in functions in R, including numeric functions like abs, sqrt, and exp, as well as string manipulation functions such as substr, grep, and sub. It also discusses user-defined functions and demonstrates how to work with strings in vectors, matrices, arrays, data frames, and lists. Examples are provided for each function to illustrate their usage.
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
You are on page 1/ 4

R Built-in Functions

R Numeric Functions
1) abs(x) :- It returns the absolute value of input x.
x<- -4
print(abs(x))
2) sqrt(x):- It returns the square root of input x.
print(sqrt(x))
3) exp(x) : It returns exponent.
print(exp(x))

4) ceiling(x) :- It returns the smallest integer which is larger than or equal to x.


y<- 4.5
print(ceiling(y))

5) floor(x) :- It returns the largest integer, which is smaller than or equal to x.


print(floor(y))

String Function
1) substr(x, start=n1,stop=n2)
It is used to extract substrings in a character vector.
a <- "987654321"
substr(a, 3, 3)
O/P
2) grep(pattern, x , ignore.case=FALSE, fixed=FALSE)
It searches for pattern in x.
st1 <- c('abcd','bdcd','abcdabcd')
pattern<- '^abc'
print(grep(pattern, st1))
O/P
3) sub(pattern, replacement, x, ignore.case =FALSE, fixed=FALSE)
It finds pattern in x and replaces it with replacement (new) text.
st1<- "England is beautiful but no the part of EU"
sub("England', "UK", st1)
O/P:
4) strsplit(x, split)
It splits the elements of character vector x at split point.
a<-"Split all the character"
print(strsplit(a, ""))
O/P:

5) tolower(x)
It is used to convert the string into lower case.
st1<- "SneHa"
print(tolower(st1))
O/P:

6) toupper(x)
It is used to convert the string into upper case.
st1<- "SneHa"
print(toupper(st1))
O/P:
i) User-defined functions
1) Strings in Vector
x <- c("Geeks", "Hello", "Welcome", "For")

2) Strings in Matrix

#Start with character string to generate an adjacency matrix from


string_in = c('apples, pears, bananas', 'apples, bananas', 'apples, pears')
#Generate a new blank matrix

blank_matrix = generate_adj_matrix(string_in)

3) Strings in Arrays

x1 <- c("Geeks", "Geeks", "Welcome", "Geeks")


x2 <- c("Geeks", "Hello", "Geeks")

result <- array(c(x1, x2), dim = c(2, 2, 2))

4) Strings in data frames

5) Strings in lists

myList <- list(1:5, "MSSQLTips", c(TRUE, FALSE, TRUE), c(3.3, 9.9, 12.2))
str(myList)

You might also like