to me, R is a little bit quirky.

installation and execution Link to heading

$ sudo apt install r-base-core

open command prompt Link to heading

$ R

run script in command line Link to heading

$ Rscript <awesome_test.R>

R-objects Link to heading

there are many type of R-objects, including

vectors/atomic vector Link to heading

atomic vector is a vector with a single object, like v<-TRUE is a atomic vector

Data TypeExampleVerify
logicalTRUE, FALSEv <- TRUE print(class(v)) it produces the following result −[1] “logical”
numeric12.3, 5, 999v <- 23.5 print(class(v)) it produces the following result −[1] “numeric”
integer2L, 34L, 0Lv <- 2L print(class(v)) it produces the following result −[1] “integer”
complex3 + 2iv <- 2+5i print(class(v)) it produces the following result −[1] “complex”
character‘a’ , ‘“good”, “TRUE”, ‘23.4’v <- “TRUE” print(class(v)) it produces the following result −[1] “character”
raw“Hello” is stored as 48 65 6c 6c 6fv <- charToRaw(“Hello”) print(class(v)) it produces the following result −[1] “raw”

very basic data types are the R-objects called vectors

very useful fn called c() meaning combine, which combines more than one element into a vector Link to heading

random accessor [] for vector Link to heading

vector element recycling Link to heading

If we apply arithmetic operations to two vectors of unequal length, then the elements of the shorter vector are recycled to complete the operations.

v1 <- c(3,8,4,5,0,11)
v2 <- c(4,11)
# V2 becomes c(4,11,4,11,4,11)

add.result <- v1+v2
print(add.result)

sub.result <- v1-v2
print(sub.result)

it gives result

[1]  7 19  8 16  4 22
[1] -1 -3  0 -6 -4  0

sort() Link to heading

lists Link to heading

list can contain different types of elements

list1 <- list(c(2, 5, 3), 21.3, sin)

use names(list_data) to name list elements Link to heading

one can merge many lists into one list by placing all the lists inside one list() function Link to heading

unlist() to convert list to vector Link to heading

matrices Link to heading

matrix is a 2d rectangular data set.

M = matrix(c('a', 'a', 'b', 'c', 'b', 'a'), nrow = 2, ncol = 3, byrow = TRUE)
print(M)

byrow is a logical. if TRUE then the input vector elements are arranged by row

it returns

     [,1] [,2] [,3]
[1,] "a"  "a"  "b" 
[2,] "c"  "b"  "a"

random accessor using [row_num: col_num] Link to heading

arrays Link to heading

while matrices are comfined to 2d, arrays can be of any number of dimensions. just simply provide dim param

a <- array(c('blue', 1), dim=c(3,3,3))

result would be

, , 1

     [,1]   [,2]   [,3]
[1,] "blue" "1"    "blue"
[2,] "1"    "blue" "1"
[3,] "blue" "1"    "blue"

, , 2

     [,1]   [,2]   [,3]
[1,] "1"    "blue" "1"
[2,] "blue" "1"    "blue"
[3,] "1"    "blue" "1"

, , 3

     [,1]   [,2]   [,3]
[1,] "blue" "1"    "blue"
[2,] "1"    "blue" "1"
[3,] "blue" "1"    "blue"

apply(x, margin, fun) to calculate across array elements. Link to heading

fun is the function to be applied across the elements of the array.

factors Link to heading

Factors are the r-objects which are created using a vector. It stores the vector along with the distinct values of the elements in the vector as labels. The labels are always character irrespective of whether it is numeric or character or Boolean etc. in the input vector. They are useful in statistical modeling.

factors are created using factor() function. the nlevels() function gives the count of levels.

# Create a vector.
apple_colors <- c('green','green','yellow','red','red','red','green')

# Create a factor object.
factor_apple <- factor(apple_colors)

# Print the factor.
print(factor_apple)
print(nlevels(factor_apple))

the result would be

[1] green  green  yellow red    red    red    green 
Levels: green red yellow
[1] 3

is.factor() is the function to make sure it is a factor Link to heading

data frames Link to heading

data frames are created using data.frame() function. dataframe is a similar concept of dataframe in python

data frames created by vectors, each vector represents a column

# Create the data frame.
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)

result is

  gender height weight Age
1   Male  152.0     81  42
2   Male  171.5     93  38
3 Female  165.0     78  26 

extracting selected rows Link to heading

emp.data <- data.frame(...)

result <- emp.data[1:2, ]

add column Link to heading

data$newcolumn <- NA

add row Link to heading

rbind(added_to, new_data)

variables Link to heading

variable CANNOT start with numbers, can only contain letters, numbers, dot and underscore; dot cannot be followed by a number.

variables can be both leftward and rightward assign.

# Assignment using equal operator.
var.1 = c(0,1,2,3)           

# Assignment using leftward operator.
var.2 <- c("learn","R")   

# Assignment using rightward operator.   
c(TRUE,1) -> var.3           

print(var.1)
cat ("var.1 is ", var.1 ,"\n")
cat ("var.2 is ", var.2 ,"\n")
cat ("var.3 is ", var.3 ,"\n")

result is

[1] 0 1 2 3
var.1 is  0 1 2 3 
var.2 is  learn R 
var.3 is  1 1 

btw, use cat() to print multiple items into a continuous print output.

list variables Link to heading

ls() to see all variables.

variable start with dot(.) will be hidden. if you want to list all, try the following:

print(ls(all.name = TRUE))

delete variables Link to heading

rm(var.3)
print(var.3)

result would be

[1] "var.3"
Error in print(var.3) : object 'var.3' not found

if you want to delete all, do the following

rm(list = ls())
print(ls())

result would be

character(0)

operators Link to heading

arithmetic operators Link to heading

all operators operate on vectors.

%% Link to heading

Give the remainder of the first vector with the second

v <- c(2, 5.5, 6)
t <- c(8, 3, 4)
print(v%%t)

it produces

[1] 2.0 2.5 2.0

%/% Link to heading

The result of division of first vector with second (quotient)

v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v%/%t)

result would be

[1] 0 1 1

logical operators Link to heading

&, |, !

for above logical operators, Each element of the first vector is compared with the corresponding element of the second vector. The result of comparison is a Boolean value.

&&, ||

for this two operators, The logical operator && and || considers only the first element of the vectors and give a vector of single element as output.

assignment operators Link to heading

  • left assign: <-, =, <<-
  • right assign: ->, ->>

miscellaneous operators Link to heading

  • :: create series span. eg: v<-2:8, the result is 2 3 4 5 6 7 8
  • %in%: check if an element belongs to a vector or not.
  • %*%: matrix multiplication

repeat loop in R Link to heading

there is a repeat key word in R, just do the same job as while. I won’t use repeat for now. here is the tutorial

next is the same with continue from other language Link to heading

function syntax Link to heading

function_name <- function(arg_1, arg_2, ...) {
   Function body 
}

string Link to heading

paste() and cat() Link to heading

for paste,

paste(..., sep = " ", collapse = NULL)

sep is optional, default is whitespace. collapse is used to eliminate the space between two strings.

paste() returns a atomic vector

cat() does a similar job, but not the SAME. have to dig deeper.

format() Link to heading

format(x, digits, nsmall, scientific, width, justify = c("left", "right", "centre", "none"))
  • x is the vector input
  • digits is the total number of digits displayed
  • nsmall is the minimum number of digits to the right of the decimal point
  • scientific is kset to TRUE to display scientific notation
  • width indicates the minimum width to be displayed by padding blanks in the beginning.
  • justify is the display of the string to left, right or center

nchar() counting number of characters. Link to heading

toupper() and tolower() Link to heading

substring(x, from, to) Link to heading

credit to Link to heading