In this R tutorial, we will learn about some built-in numeric functions and character functions that R provides. We will see their syntaxes and usages. We will also look at examples of these numeric and character functions in R.
What are Numeric Functions?
There are functions in R, that perform operations on specific data types. The functions that take a numeric value or vector as input or return them as outputs are called numeric functions.
In this tutorial, we are going to be looking at the following numeric functions:
- is.numeric() and as.numeric() functions
- abs() function
- ceiling() and floor() functions
- exp() function
- log() function
- round() function
- sqrt() function
- trigonometric functions
- factorial() function
- sign() function
1. is.numeric() and as.numeric() Functions
The is.numeric()
function takes an object as an input argument and returns TRUE
if the elements are of numeric type. For example:
But to implement functions thoroughly you must have clear understanding of vectors in R.
Code:
numvec <- c(11,23,52,12,4,34,27,45) is.numeric(numvec)
Output:
The as.numeric()
function coerces the input values to the numeric data type. For example:
Code:
intvec <- c(1L,2L,3L,4L) charvec <- c("a","b","c","d") logvec <- c(T,F,T,F,F,T,T,T) compvec <- c(12+3i,6i,5+2i,4+9i) as.numeric(intvec)
Output:
Code:
as.numeric(charvec)
Output:
Warning message:
NAs introduced by coercion
Code:
as.numeric(logvec)
Output:
Code:
as.numeric(compvec)
Output:
[1] 12 0 5 4
Warning message:
imaginary parts discarded in coercion
Note: When characters are coerced into numeric data type, the result is NA
. When complex values are coerced into numeric, their imaginary parts are removed.
2. abs() Function
The abs()
function returns the absolute value for the input numeric value. For example:
Code:
abs(-3.14)
Code:
abs(c(14.4,-55.5,34.76,45.4,-4.34,-5.56,-45.13))
Output:
3. ceiling() and floor() Functions
The ceiling()
function returns the lowest number that is greater than the input numeric value. The floor()
function returns the largest number that is smaller than the input value. For example:
Code:
ceiling(c(14.4,-55.5,34.76,45.4,-4.34,-5.56,-45.13))
Code:
floor(c(14.4,-55.5,34.76,45.4,-4.34,-5.56,-45.13))
Output:
4. exp() Function
The exp()
function returns the exponential value ‘e’ raised to the power of the input numeric value. For example:
Code:
exp(5)
Output:
5. log() Function
The log()
function returns the logarithmic value for the input numeric values. For example:
Code:
log(14)
Code:
log(14,base=2)
Output:
6. round() Function
The round()
function rounds the given input numeric values to the number of specified digits. For example:
Code:
round(4.55231,digits=2)
Output:
7. sqrt() Function
The sqrt()
function returns the square root of the input numeric value. For example:
Code:
sqrt(225)
Code:
sqrt(9)
Code:
sqrt(37)
Output:
8. Trigonometric Functions
The trigonometric functions like cos()
, sin()
, tan()
, etc. return the values of the trigonometric functions for the given input values. For example:
Code:
sin(34)
Code:
cos(65)
Code:
tan(23)
Output:
9. factorial() Function
The factorial()
function returns the factorial values of the given input numeric value. For example:
Code:
factorial(5)
Code:
factorial(13)
Output:
10. sign() Function
The sign()
function returns -1
if the input argument is negative in nature and +1
if the number is positive. For example:
Code:
sign(-5)
Code:
sign(10)
Output:
What are Character Functions?
Vector Functions in R that take a character value or vector as input or return them as output are called character functions.
Here are a few character functions that we are going to study today:
- is.character() and as.character() functions
- substr() function
- strsplit() function
- toupper() and tolower() functions
- paste() function
- nchar() function
- sub() function
- abbreviate() function
- trimws() function
- grep() function
1. is.character() and as.character() Functions
The is.character()
function returns TRUE
if the input value or object is of the character data type. The as.character()
function converts the input values into the character data type. For example:
Code:
is.character("hello")
Code:
is.character(c("this","is","a","character","vector"))
Output:
Code:
as.character(1234)
Code:
is.character(c(1,2,3,4,5,5))
Output:
2. substr() Function
The substr()
function extracts a substring from a given string based on the start
and stop
arguments. For example:
Code:
substr("this is a string",start=3,stop=12)
Output:
Any queries in R numeric and character functions article till now? Ask TechVidvan experts in comments.
3. strsplit() Function
The strsplit()
function splits a string or vector of strings based on a regular expression or a fixed string. For example:
Code:
strsplit("this is a string",split=" ")
Output:
4. toupper() and tolower() Functions
The toupper()
function in R converts the input string or strings to the upper-case. The tolower()
converts them to the lower-case. For example:
Code:
strings <- c("these","strings","are","in","lower-case") toupper(strings2)
Code:
strings2 <- c("THIS","IS","A","CHARACTER","VECTOR") tolower(strings)
Output:
5. paste() Function
The paste()
function converts vectors into characters (if needed), and concatenates them into a character string or character vector. For example:
Code:
x1_5 <- paste("x",1:5,sep="_") x1_5
Code:
coll_strings <- paste(strings,collapse=" ") > coll_strings
Output:
Note: In the first example, the paste()
function coerces the numeric values to characters and concatenates them to x with the sep
between them. In the second example, the paste()
function collapses the elements into a single string with the collapse
argument between them.
6. nchar() Function
The nchar()
function counts the number of characters in a string. For example:
Code:
nchar(coll_strings)
Output:
7. sub() Function
The sub()
function replaces the first instance of a substring from a given string. For example:
Code:
strings
Code:
sub("lower-case","a vector",strings)
Output:
8. abbreviate() Function
The abbreviate()
function abbreviates the strings inside the given character vectors. We can specify the minimum length of the abbreviations and also whether or not to use dots for each abbreviation. For example:
Code:
countries <- c("greenland","united kingdom","united states","india","japan") > countries_abb <- abbreviate(countries) > countries_abb
Output:
9. trimws() Function
The trimws()
function removes leading or trailing whitespaces in a string. For example:
Code:
stringws <- " hello " > stringws <- trimws(stringws) > stringws
Output:
10. grep() Function
The grep()
function searches for a specific pattern of string in each element of a character vector. It returns the index of the elements containing the target string or pattern. For example:
Code:
strings
Code:
grep("a",strings)
Output:
Summary
Today, we learned about various useful R functions that take inputs or return outputs as numeric or character values or vectors. These functions are important for calculations or string manipulations. They help us in performing operations on numeric or character variables in datasets.
After reading this TechVidvan tutorial, you should be able to use the above-listed functions. All you need is practice.
Any difficulty while practicing numeric and character functions in R programming?
Ask our TechVidvan experts.
Keep practicing!!