DEV Community

Irfan Faisal
Irfan Faisal

Posted on • Edited on

R Programming basics

Quick notes on R

Datatypes

R

typeof(x) or class(x)– both returns the data type of variable. however, typeof(x) is tend to be more specific

x<-23.5 print(class(x)) >>>Numeric print(typeof(x)) >>>double 
Enter fullscreen mode Exit fullscreen mode

length(x)– returns the length of a vector (list) or other objects: 1, or any other positive integer number.

print(x)– prints the value of x in console

R Objects

1.Vector: one-dimensional array containing elements of the same data type.

apple <- c('red','green',"yellow") #all character data types print(apple) >>>red, green, yellow 
Enter fullscreen mode Exit fullscreen mode

2.List: ordered collection of elements that can be of different data types.

list1 <- list(c(2,5,3),21.3,5L) #contains vector, numeric, integer print(list1) 
Enter fullscreen mode Exit fullscreen mode

3.Matrix: a two-dimensional array where all elements must be of the same data type.

M = matrix( c('a','a','b','c','b','a'), nrow = 2, ncol = 3, byrow = TRUE) print(M) >>> [,1] [,2] [,3] [1,] "a" "a" "b" [2,] "c" "b" "a" 
Enter fullscreen mode Exit fullscreen mode

4.Array: a multi-dimensional data structure where all elements must be of the same data type.

a <- array(c('green','yellow'),dim = c(3,3,2)) print(a) [,1] [,2] [,3] [1,] "green" "yellow" "green" [2,] "yellow" "green" "yellow" [3,] "green" "yellow" "green" , , 2 [,1] [,2] [,3] [1,] "yellow" "green" "yellow" [2,] "green" "yellow" "green" [3,] "yellow" "green" "yellow" 
Enter fullscreen mode Exit fullscreen mode

5.Data Frame: a two-dimensional, table-like structure where each column can hold data of a different type, making it similar to a spreadsheet or a SQL table.

BMI<-data.frame( gender = c("Male", "Male","Female"), height = c(152, 171.5, 165), weight = c(81,93, 78), Age = c(42,38,26) ) print(BMI) >>> gender height weight Age 1 Male 152.0 81 42 2 Male 171.5 93 38 3 Female 165.0 78 26 
Enter fullscreen mode Exit fullscreen mode

Operators

1.+: adds two vectors.

v <-c( 2,5.5,6) t <-c(8, 3, 4) print(v+t) >>[1] 10.0 8.5 10.0 
Enter fullscreen mode Exit fullscreen mode

2.-: Subtracts second vector from the first

v <-c( 2,5.5,6) t <-c(8, 3, 4) print(v-t) >>[1] -6.0 2.5 2.0 
Enter fullscreen mode Exit fullscreen mode

3.*: Multiplies both vectors

v <-c( 2,5.5,6) t <-c(8, 3, 4) print(v*t) >>[1] 16.0 16.5 24.0 
Enter fullscreen mode Exit fullscreen mode

4./: Divide the first vector with the second

v <-c( 2,5.5,6) t <-c(8, 3, 4) print(v/t) >>[1] 0.250000 1.833333 1.500000 
Enter fullscreen mode Exit fullscreen mode

5.%%: Give the remainder of the first vector with the second

v <-c( 2,5.5,6) t <-c(8, 3, 4) print(v%%t) >>[1] 2.0 2.5 2.0 
Enter fullscreen mode Exit fullscreen mode

6.%/%: The result of division of first vector with second (quotient)

v <-c( 2,5.5,6) t <-c(8, 3, 4) print(v%/%t) >>[1] 0 1 1 
Enter fullscreen mode Exit fullscreen mode

7.^: The first vector raised to the power of second vector

v <-c( 2,5.5,6) t <-c(8, 3, 4) print(v^t) >>[1] 256.000 166.375 1296.000 
Enter fullscreen mode Exit fullscreen mode

Relational Operators

1.>: Checks if each element of the first vector is greater than the corresponding element of the second vector

v <-c(2,5.5,6,9) t <-c(8,2.5,14,9) print(v>t) >>[1] FALSE TRUE FALSE FALSE 
Enter fullscreen mode Exit fullscreen mode

2.<: Checks if each element of the first vector is less than the corresponding element of the second vector.

v <-c(2,5.5,6,9) t <-c(8,2.5,14,9) print(v < t) >>[1] TRUE FALSE TRUE FALSE 
Enter fullscreen mode Exit fullscreen mode

3.==: Checks if each element of the first vector is equal to the corresponding element of the second vector.

v <-c(2,5.5,6,9) t <-c(8,2.5,14,9) print(v == t) >>[1] FALSE FALSE FALSE TRUE 
Enter fullscreen mode Exit fullscreen mode

4.<=: Checks if each element of the first vector is less than or equal to the corresponding element of the second vector.

v <-c(2,5.5,6,9) t <-c(8,2.5,14,9) print(v<=t) >>[1] TRUE FALSE TRUE TRUE 
Enter fullscreen mode Exit fullscreen mode

5.!=: Checks if each element of the first vector is unequal to the corresponding element of the second vector.

v <-c(2,5.5,6,9) t <-c(8,2.5,14,9) print(v!=t) >>[1] TRUE TRUE TRUE FALSE 
Enter fullscreen mode Exit fullscreen mode

Miscellaneous Operators

1.:: Colon operator. It creates the series of numbers in sequence for a vector.

v <-1:8 print(v) >>[1] 1 2 3 4 5 6 7 8 
Enter fullscreen mode Exit fullscreen mode

2.%in%: This operator is used to identify if an element belongs to a vector.

v1 <-8 v2 <-12 t <-1:10 print(v1 %in% t) print(v2 %in% t) >>[1] TRUE [1] FALSE 
Enter fullscreen mode Exit fullscreen mode
  1. %*%: This operator is used to multiply a matrix with its transpose.
M = matrix( c(2,6,5,1,10,4), nrow= 2,ncol = 3,byrow = TRUE) Q = M %*% t(M) print(Q) >> [,1] [,2] [1,] 65 82 [2,] 82 117 
Enter fullscreen mode Exit fullscreen mode

Condition

x <- c("what","is","truth") if("Truth" %in% x) { print("Truth is found the first time") } else if ("truth" %in% x) { print("truth is found the second time") } else { print("There is no Truth in your code ") } >>[1] "truth is found the second time" 
Enter fullscreen mode Exit fullscreen mode

For Loop

A For loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.

a=c(1:5) for ( i in a) { print(i) } >> 1 2 3 4 5 
Enter fullscreen mode Exit fullscreen mode

While Loop

The While loop executes the same code again and again until a stop condition is met.

v ="Hello, while loop" cnt = 2 while (cnt < 7) { print(v) cnt = cnt + 1 } >> [1] "Hello, while loop" [1] "Hello, while loop" [1] "Hello, while loop" [1] "Hello, while loop" [1] "Hello, while loop" 
Enter fullscreen mode Exit fullscreen mode

Next: to skip the current iteration of a loop without terminating it.

Break: the loop is immediately terminated

Basic Functions

1) readline(): to get input from user
2) as.integer(): converts into integer
3) as.numeric(): converts into numeric

Top comments (1)

Collapse
 
tyrrx profile image
David R.

Just use Python