R Vector Functions – How to apply Functions over R Vectors

We are going to discuss R vector functions in this tutorial. They are the functions that can be applied over R vectors. We will explain all the functions with detailed examples to provide you with better command over the concept.

R Vector Functions

What are R Vector Functions?

First of all, let’s discuss what exactly a function means. In order to perform a particular task, we make use of the function, which is a piece of code in R.  In R, these functions are referred to as the objects as they facilitate the similar way of functioning like other types of objects.  Functions that we use in R vectors are known as the vector functions.

For example: rep(), seq(), using all() and any(), more on c() etc.

Wait! Have you checked – R Matrix Functions

Here we are going to discuss all these functions of the R vector in detail with examples.

1. R rep() Function

rep() is used for replicating the values in x. The two common cases that exhibit faster-simplified versions are rep.int and rep_len. Furthermore, these functions are not generic.

Let’s now discuss how we can apply this rep() to any vector with the help of examples. 

1.1. How to repeat vectors in R

You can use the rep() function in several ways if you want to repeat the complete vector.

For example:

a) To repeat the vector c(0, 0, 7) three times, use this code:

> rep(c(0, 0, 7), times = 4)
[1] 0 0 7 0 0 7 0 0 7 0 0 7

b) We can also repeat every value by specifying each argument, like this:

> rep(c(2, 4, 2), each = 2)
[1] 2 2 4 4 2 2

c) For each value, we can tell R how often it has to repeat:

> rep(c(0, 7), times = c(4,3))
[1] 0 0 0 0 7 7 7

d) In seq, we use the argument, length.out to define R. It will repeat the vector until it reaches the specified length, even if the last repetition is incomplete.

> rep(1:3,length.out=9)
[1] 1 2 3 1 2 3 1 2 3

Output:

rep vector c

You should definitely check the R Recursive Function Tutorial

2. R seq() Function

It generates regular sequences. The seq function is standard generic in nature that also has a default method. seq.int poses a few restrictions due to its primitiveness but it is also much faster. The two common cases that exhibit fast primitives are seq_along and seq_len.

Let’s now discuss how we can apply this seq() to any vector with the help of examples.

2.1. How to create vectors in R

a) In order to use integers to create vectors:

For example:

To create a list of vectors over a specified range, we use the colon (:) symbol.

The code 1:5 gives you a vector with the numbers 1 to 5, and 2:–5 create a vector with the numbers 2 to –5.

b) Using the seq(), we make steps in a sequence. Seq() function is used to describe the intervals by which numbers should decrease or increase.

For example:

In R, a vector with numbers 4.5 to 3.0 in steps of 0.5.

> seq(from = 4.5, to = 3.0, by = -0.5)
[1] 4.5 4.0 3.5 3.0

c) You can specify the length of the sequence by using the argument, length.out. Afterwards, R can calculate the step size by itself.

For example:

You can make a vector of nine values going from -2.7 to 1.3 like this:

> seq(from = -2.7, to = 1.3, length.out = 9)
[1] -2.7 -2.2 -1.7 -1.2 -0.7 -0.2 0.3 0.8 1.3

Output:

seq function - R vector function

Do you know about Numeric and Character Functions in R

3. R any() Function

It takes the set of vectors and returns a set of logical vectors, in which at least one of the value is true.

Join DataFlair on Telegram

3.1. Usage of R any() Function

Check if any or all the elements of a vector are TRUE. Both functions also accept many objects.

any(…, na.rm=FALSE)

Here,

  •  means one or more R objects that need to be checked.
  • na.rm means state whether NA values should be ignored or not.

4. R all() Function

It takes the set of vectors and returns a set of logical vectors, in which all of the values are TRUE.

4.1. Usage of R all() Function

all(…, na.rm=FALSE)

Here,

  •  means one or more R objects that need to be checked.
  • na.rm means state whether NA values should be ignored or not.

The any() and all() functions are shortcuts because they report any or all of their arguments as TRUE.

Let’s see this by example:

> #Author DataFlair
> x <- 1:10
> any(x > 5)
[1] TRUE
> any(x > 88)
[1] FALSE
> all(x > 88)
[1] FALSE
> all(x > 0)
[1] TRUE

Output:

any and all function - R vector function

Suppose that R executes the following:

any(x > 5)

It first evaluates x > 5:

(FALSE, FALSE, FALSE, FALSE, FALSE)

any() function reports whether any of those values are TRUE, while all() function works and reports if all the values are TRUE.

Take a deep insight into Arguments in R programming

R’s C interface

R’s source code is a powerful technique for improving programming skills. But, many base R functions were already written in C.

R is used to figure out how those functions work. All functions in R are defined with the prefix, Rf_ or R_.

Outline of R’s C interface

  • Input validations state about itself so that C function doesn’t crash R.
  • C data structure shows how to translate data structure names from R to C.
  • In order to create, change and make vectors in C, we make use Creating and Modifying vectors.
  • Calling C also defines the functions with the inline package.

Prerequisites to Working on C Interface

We need a C compiler for C interface. Windows users can use Rtools whereas Mac users will need the Xcode command line tools. And, most Linux distributions will come with the necessary compilers.

In Windows, it is necessary to include the Windows PATH environment variable in it.

  • Rtools executables directory – (C:\Rtools\bin)
  • C compiler executables directory – (C:\Rtools\gcc-4.6.3\bin)

Calling C Functions from R

Generally, to call a C function, it requires two pieces:

  • C function
  • R wrapper function that uses, Call()

We implement our function f(x)= 2x in the file, doubling.c through the following lines of code:

// Author DataFlair
void doubler(int* mem) {
 
 *mem = *mem + *mem;
}

Code Display:

void doubler C code - R Vector Function

Run the following command in your terminal:

R CMD SHLIB doubling.c

Output:

Doubling Command - R vector function

On the R interactive session, type:

#Author DataFlair
dyn.load("doubling.so")
.C("doubler", x = as.integer(5))

Output:

dyn load R Code - R vector functions

Summary

We have discussed vectors and functions over the vectors in detail. A vector is a type of datatype and has its own importance. But, using a datatype with function totally changes its meaning and use. We have to develop a code using function only once and you can use that code anytime. There is no need to write code, again and again, as the larger program will store it in two or more functions.

If you have any queries related to R vector functions, let us know in the comment section below.

Now, cracking R Interview is easy from these R Interview Questions and Answers

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.