Codementor Events

Learning R | Part 2 | Variables & Functions

Published Jun 03, 2019


Photo by Markus Spiske on Unsplash


Variables

  • x <- (1:10)
  • x = (1:10)
  • (1:10) -> x
  • assign(“x”, 1:10)

All of the above ways assign an array from 1 to 10 to variable x.

These are the assigning operators =,->, <-.

Another common function for creating a vector or a list is the c(…) function.

  • c(1,10:13) The output for this would be an array 1 10 11 12 13. This means merging of comma separated objects/variables.
  • c(1:5, 10.5, “next”) The output for this would be an array “1” “2” “3” “4” “5” “10.5” “next”.
  • y = c(1,2,3) & x = c(y, 1, y) This is an example of using variables to create new variables. Over here the value of y would be 1 2 3 while that of x would be 1 2 3 1 1 2 3.

For further usage of c(…) function refer-

c function | R Documentation
_This is a generic function which combines its arguments. The default method combines its arguments to form a vector…_www.rdocumentation.org


Built-in Functions

  • c(…)_— _As explained above.
  • ls() or objects()_ — _Gives a list of existing objects that are made.
  • rm(“x”)_ — _Deletes an object. Parameter being name of the object.
  • sum(“x”)_ — Gives the sum of the vector x.
    For example, if_ x is 1 2 3 1 1 2 3then sum(“x”) would be13.
  • sqrt(“x”)_ — _Gives square root of the vector x.
    F or example if x is 1 4 9 then sqrt(“x”) would be another vector with values 1 2 3.
  • seq(…)_ — _This function is used to generate a sequence of numbers. It takes different parameters like from (Start value of sequence), to (End value of sequence), by (Number by which the sequence is to be incremented), length.out (Length of sequence) and along.with _(_This is a list of vector with length n and is used only to get the length of this passed list. Weird 😷)
    Example:- seq(from=1, to=4, by=0.5) — Gives a sequence from 1 to 4 with 0.5 increment.
    More on seq(…) here.
  • paste(…) This function is used to make strings using concatenation of vectors using 2 parameters namely sep & collapse. sep provides a separator between the concatenated vectors while collapse provides a separator which concatenates the values in the concatenated vector. (Inception? 🤒) Let’s learn it with some examples.
    paste(“xyz”, 1:3)“xyz 1” “xyz 2” “xyz 3” (This is without sep & collapse)
    paste(“xyz”, c(1,2,”variable”,3), sep=”,”) —“xyz,1” “xyz,2” “xyz,variable” “xyz,3” (This is only with sep )
    paste(c(1:5), c(5:10), sep = “: “, collapse = “; “) — “1: 5; 2: 6; 3: 7; 4: 8; 5: 9; 1: 10” (This is with sep and collapse; Notice here that this forms a single string 😄.)
    More on paste(…) here.
  • rep(x, …)_ —_ As the name suggests, this function is used to repeat the existing vector. The parameters this function can take are times (Number of times the vector should be repeated), length.out (Desired length of output vector) & each (Number of times each element of the vector should be repeated
    rep(c(1,2,3), 3) or rep(c(1:3), times=3)1 2 3 1 2 3 1 2 3
    rep(c(1:3), each=3)1 1 1 2 2 2 3 3 3More on rep(x, …) here.

User Defined Functions

The below example shows a simple way to write a single line function which returns the square of a variable. The function name over here is fn.

fn <- function(a) {a*a}

fn(10)

Example for a block of code in a function.

fn -> function(a, b) { c = a * b c = c + b print(x)}

fn(10, 20)

Functions with loops,if-else.

primeNumber = function(n) {

if(n>=2) {

s = seq(2,n) p = c() #Initialising the vector which stores prime numbers

for(i in seq(2,n)) { if(any(s == i) { p = c(p, i) s = c(s[(s%%i) != 0], i) } } return(p) } else { stop("Input greater than 2") }}

The above example returns a vector with all the prime numbers upto n passed to the function. The stop function here stops the execution and throws an error.

The initial p = c() is used to initialise the vector which stores the prime number. And s = seq (2,n) is used to initialise the vector till n, which is then looped.

The loop checks if the value is present in the vector s defined earlier using the any function. And if it matches then it appends that value to the prime vector and updates the vector to be checked with i.e s with all the numbers that are not divisible with the current i

At the end of the loop, the p vector contains all the prime numbers.


References


Thanks for reading. In my next article, I’ll be explaining about plot functions . Apart from that, some insights on packages & datasets.

Also, if you haven’t read the part 1. You can read it here.

Drop your questions below. Suggestions are welcomed. 🙌

Discover and read more posts from Jigar Mehta
get started
post commentsBe the first to share your opinion
Show more replies