Skip to content

Cheatsheet-lang/R

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 

Repository files navigation

R

R-Cheatsheet

R is similar to python with some differences in syntax

Packages

Installing packages

install.packages('package_name')

Loading Package

library(package_name)         #notice - no quotes

Basic Operations

Variables

a = 10
b = "hello"
c <- "similar to above one"     # "<-" is similar to '='

Arithmetic Operations

10 + 11
19 * 7
7 / 3
5 ^ 3   #125 - exponential
5 ** 3  #125 - exponential
8 %% 5  #3 - %% indicates x mod y (“x modulo y”)
8 %/% 5 #1 - Integer Division

Conditions

if - else

if (condition){
    Do something
} else {
    Do something different
}

Loops

for

for (variable in sequence){
    Do something
}

Example

for (i in 1:3){
    print(i)
}

output

1
2
3

while

while (condition){
    Do something
}

Example

while (i < 10){
    print(i)
    i = i + 1
}

Vectors

Creating Vector

v <- c(2, 4, 6)      # 2, 4, 6
2:6             # 2, 3, 4, 5, 6

Operations

  • Length
length(v)
  • Indexing
v[1:3]
  • Boolean
all(v)
any(v)

Dataframes

Creating Dataframe

df <- data.frame(col1 = v1, col2 = v2)