Learning R | Part 2 | Variables & Functions
Photo by Markus Spiske on Unsplash
Variables
x <- (1:10)x = (1:10)(1:10) -> xassign(“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 array1 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 ofywould be1 2 3while that ofxwould be1 2 3 1 1 2 3.
For further usage of c(…) function refer-
Built-in Functions
c(…)_— _As explained above.ls()orobjects()_ — _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 vectorx.
For example, if_xis1 2 3 1 1 2 3thensum(“x”)would be13.sqrt(“x”)_ — _Gives square root of the vectorx.
F or example ifxis1 4 9thensqrt(“x”)would be another vector with values1 2 3.seq(…)_ — _This function is used to generate a sequence of numbers. It takes different parameters likefrom(Start value of sequence),to(End value of sequence),by(Number by which the sequence is to be incremented),length.out(Length of sequence) andalong.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 from1to4with0.5increment.
More onseq(…)here.paste(…)This function is used to make strings using concatenation of vectors using 2 parameters namelysep&collapse.sepprovides a separator between the concatenated vectors whilecollapseprovides 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 withoutsep&collapse)
paste(“xyz”, c(1,2,”variable”,3), sep=”,”)—“xyz,1” “xyz,2” “xyz,variable” “xyz,3”(This is only withsep)
paste(c(1:5), c(5:10), sep = “: “, collapse = “; “)—“1: 5; 2: 6; 3: 7; 4: 8; 5: 9; 1: 10”(This is withsepandcollapse; Notice here that this forms a single string 😄.)
More onpaste(…)here.rep(x, …)_ —_ As the name suggests, this function is used to repeat the existing vector. The parameters this function can take aretimes(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 onrep(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. 🙌
