R Vector Functions – List of Functions Apply over R Vectors

1. Objective

In our previous R tutorial, we have discussed what is R data types and R Functions in detail. Now in this tutorial, we are going to discuss R vector Functions that means the functions that can be applied over R vectors. Such functions are rep(), seq(), using all() and any(), more on c() etc. We will also explain all the functions with the detailed example.

R Vector Functions - List of Functions Apply over R Vectors

R Vector Functions – List of Functions Apply over R Vectors

2. What are R Vector Functions?

First of all, we will discuss what exactly Function Means. In R, a function is a piece of code written to carry out a specified task. R Functions are called as objects because you can work with them exactly the same way you work with any other type of object. R Vector functions are those functions which we use in R vectors.
For Example: rep(), seq(), using all() and any(), more on c() etc.
Here we are going to discuss all these functions of R vector in detail with examples.

2.1. R rep() Function

rep() replicates the values in x. It is a generic function. rep.int and rep_len are faster-simplified versions for two common cases. They are not generic.

Usage of rep() function in R-

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

2.1.1. How to repeat vectors in R

You can use the rep() function in several ways if we 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 the argument each, like this:
> rep(c(2, 4, 2), each = 2)
[1] 2 2 4 4 2 2

c) We can tell R for each value 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 that length, even if the last repetition is incomplete.
> rep(1:3,length.out=9)
[1] 1 2 3 1 2 3 1 2 3

2.2. R seq() Function

It Generates regular sequences. seq is a standard generic with a default method. seq.int is a primitive which can be much faster but has a few restrictions. seq_along and seq_len are very fast primitives for two common cases.
Usage of seq() function in R-
Let’s now discuss how we can apply this seq() to any vector with the help of examples.

2.2.1. How to create vectors in R

a) To create a vector using integers:
For Example:
We use the colon operator (:) in R.
The code 2:6 gives you a vector with the numbers 2 to 6, and 3:-4 create a vector with the numbers 3 to –4, both in steps of 1.
b) We use the seq() to make steps in a sequence. Seq() function is used to describe by which the numbers should decrease or increase.
For example:
In R, a vector with a 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 out. R calculates the step size 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

2.3. R any() Function

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

2.3.1. Usage of R any() Function

Check whether any or all elements of a vector are TRUE. Both functions also accept many objects.
any(…, na.rm=FALSE)
here,

  • – One or more R objects that need to be check.
  • na.rm – State whether NA values should ignore.

2.4. R all() Functions

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

2.4.1. Usage of R all() Function

all(…, na.rm=FALSE)
here,

  • – one or more R objects that need to be check.
  • na.rm – State whether NA values should ignore.

The any() and all() functions are shortcuts because they report any or all their arguments are TRUE.
Let’s see this by example-

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

For example:
Suppose that R executes the following:
> any(x > 5)
It first evaluates x > 5:
(FALSE, FALSE, FALSE, FALSE, FALSE)
We use any() function – that reports whether any of those values are TRUE while all() function works and reports if all the values are TRUE.

3. 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 been used to figure out how those functions work. All functions in R defined with the prefix Rf_ or R_.

3.1 Outline of R’s C interface

  • Input Validations talks about itself so that C function doesn’t crash R.
  • C data Structures shows how to translate data structure names from R to C.
  • Creating and modifying vectors teaches how to create, change, and make vectors in C.
  • Calling C defines the basics of creating. It also defines the functions with the inline package.

3.2. Prerequisites

We need a C compiler for C interface. Windows users can use Rtools. Mac users will need the Xcode command line tools. Most Linux distributions will come with the necessary compilers.
In Windows, it is necessary to include Windows PATH environment variable in it.

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

3.3. Calling C functions from R

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

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

The function below adds two numbers together:

// In C ----------------------------------------
#include <R.h>
#include <Rinternals.h>
SEXP add(SEXP a, SEXP b) {
SEXP result = PROTECT(allocVector(REALSXP, 1));
REAL(result)[0] = asReal(a) + asReal(b);
UNPROTECT(1);
return result;
}
# In R ----------------------------------------
add <- function(a, b) {
.Call("add", a, b}
}

4. Conclusion

We have discussed vectors and functions over the vectors in detail. A vector is a type of a datatype and has its own importance but using a datatype with a function totally changes its mean and its use. As we have learned in this tutorial what is function and how it is applied over datatypes. Using a function we have to develop a code only once and can use that code anytime, doesn’t need to write code, again and again, the large program will store in two or more functions.
if you like this blog, or you have any query related to R vector functions, so let us know by leaving a comment in a section given below.
See Also-

Reference for R 

Leave a Reply

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