Numeric and Character Functions in R – Explore Inbuilt Functions with Examples

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:

  1. is.numeric() and as.numeric() functions
  2. abs() function
  3. ceiling() and floor() functions
  4. exp() function
  5. log() function
  6. round() function
  7. sqrt() function
  8. trigonometric functions
  9. factorial() function
  10. 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:

is.numeric() - numeric and character functions in r

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:

[1] 1 2 3 4

Code:

as.numeric(charvec)

Output:

[1] NA NA NA NA
Warning message:
NAs introduced by coercion

Code:

as.numeric(logvec)

Output:

[1] 1 0 1 0 0 1 1 1

Code:

as.numeric(compvec)

Output:

[1] 12 0 5 4

Warning message:
imaginary parts discarded in coercion

as.numeric() - numeric and character functions in r

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:

abs() - numeric and character functions in r

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:

numeric and character functions in r - ceiling() and floor()

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:

exp() - numeric and character functions in r

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:

log() - numeric and character functions in r

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:

round() - numeric and character functions in r

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:

sqrt() - numeric and character functions in r

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:

trigonometric functions - numeric and character functions in r

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:

factorial() - numeric functions in R

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:

sign() - numeric functions in r

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:

  1. is.character() and as.character() functions
  2. substr() function
  3. strsplit() function
  4. toupper() and tolower() functions
  5. paste() function
  6. nchar() function
  7. sub() function
  8. abbreviate() function
  9. trimws() function
  10. 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:

is.character() - character functions in r

Code:

as.character(1234)

Code:

is.character(c(1,2,3,4,5,5))

Output:

as.character() - numeric and character functions in r

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:

substr() - numeric and character functions in r

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:

strsplit() - character functions in r

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:

toupper() and tolower() - character functions in r

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:

paste() - numeric and character functions in r

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:

nchar() - character functions in r

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:

sub() - numeric and character functions in r

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:

abbreviate() - numeric and character functions in r

9. trimws() Function

The trimws() function removes leading or trailing whitespaces in a string. For example:

Code:

stringws <- " hello "
> stringws <- trimws(stringws)
> stringws

Output:

 trimws() -character functions in r

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:

grep() - numeric and character functions in r

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

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.