Learning R | Part 2 | Variables & Functions
Learning R | Part 2 | Variables & Functions
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 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 ofy
would be1 2 3
while that ofx
would 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. The parameter being the name of the object.sum(“x”)
— Gives the sum of the vectorx
.
For example, ifx
is1 2 3 1 1 2 3
thensum(“x”)
would be13
.sqrt(“x”)
— Gives the square root of the vectorx
.
For example ifx
is1 4 9
thensqrt(“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 from1
to4
with0.5
increment.
More onseq(…)
here.paste(…)
This function is used to make strings using a concatenation of vectors using 2 parameters namelysep
&collapse
.sep
provides a separator between the concatenated vectors whilecollapse
provides a separator that 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 withsep
andcollapse
; 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 3
More onrep(x, …)
here.
User-Defined Functions
The below example shows a simple way to write a single line function that returns the square of a variable. The function name over here is fn
.
fn <- function(a) {a*a}
fn(10)
Example of 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 up to n
passed to the function. The stop
function here stops the execution and throws an error.
The initial p = c()
is used to initialize the vector which stores the prime number. And s = seq (2,n)
is used to initialize the vector till, which is then looped.
The loop checks if the value is present in the vector s
defined earlier using 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 plot functions. Apart from that, some insights on packages & datasets.
Also, if you haven’t read part 1. You can read it here.
Drop your questions below. Suggestions are welcomed. 🙌