R Cheatsheet Cheatsheet




R is very popular for data analytics which was created by Ross Ihaka and Robert Gentleman in 1993. Many big companies like Google, Facebook, Airbnb etc uses this language for data analytics.

Basics

Sample program

print("Hello, World!")
  • print -- It is used to display the data to the console.
  • cat -- you can also use cat to display data to console.
  • # -- is used to comment a line in R and it does not support multi-line comments.

Data types

There are six data types of the atomic vectors:

Data typeDescriptionUsage
NumericTo represent decimal valuesx=1.84
IntegerTo represent integer values, L tells to store the value as integerx=10L
ComplexTo represent complex valuesx = 10+2i
LogicalTo represent boolean values, true or falsex = TRUE
CharacterTo represent string valuesx <- "One compiler"
rawHolds raw bytes

Variables

var-name = value
var-name <- value
value -> var-name

Example

var_logical <- FALSE  # Logical variable
  
var_numeric -> 797 # Numeric variable
  
var_integer = 53L  # integer variable
  
var_complex <- 5+2i  # complex variable
  
var_char<- "One Compiler"  # character variable
  
var_raw <- charToRaw("Hello World")  # raw variable

Operators

Operator typeDescription
Arithmetic Operator+ , - , * , / , %%, ^
Relational Operator< , > , <= , >=, != , ==
Logical Operator&, |, && , ||, !
Assignment Operator= , ->, ->>, <-, <<-
Misc Operators:, %in%, %*%

Strings

' or " are used to enclose strings in R.

str1 <- "Hello World!"
print(str1)
str2 <- 'Happy learning!!'
print(str2)
String FunctionDescription
paste()Strings are concatenated using paste() function
format()This function is used to format Numbers and strings to a specific style.
nchar()This function is used to count the number of characters including spaces in the given string.
substring()This function is used to extract part of a string
toupper()Converts given string to uppercase
tolower()Converts given string to lowercase

Vectors

Vectors are considered as basic R objects.

Single Element Vectors

x <- "s" #Character type atomic vector
x <- 53.2 #double type atomic vector
x <- 79L #integer type atomic vector
x <- TRUE # Logical type atomic vector
x <- 2+5i# complex type atomic vector
x <- charToRaw('OneCompiler') #  raw type atomic vector.

2. Multiple Elements vector

x <- 1:10 # Creating a sequence from 1 to 10 using colon
x <- 5.5:10.5 # Creating a sequence of decimal values using colon
x <- seq(1, 5, by = 0.5) # creating sequence using seq()
x <- c('hello', 0, 3, TRUE) # mixed type vector

Lists

List is a R-Object which can contain elements of different types. list() function is used to create a list.

emp_list <- list( c("Foo","Bar", "Alex", "Mark"), c(1,2,3,4)) # creating a list

names(emp_list) <- c("Names","Id") # assigning name to the list items

print(emp_list)

Arrays

array() function is used to create an array in R.

arrayName <- array(data, dim= (rowSize, columnSize, matrices, dimNames))  
  • data : data is an input vector
  • rowSize : defines no of row elements array can store
  • columnSize : defines no of column elements array can store
  • dimNames : specifies row and column names
  • matrices : array can consists of multi-dimensional matrices

Example

x <- c(1,2,3)
y <- c(4,5,6,7,8,9)

arr <- array(c(x,y),dim=c(3,3,3)) # 3 rows, 3 columns and 3 matrices

print(arr[,,3]) # prints 3rd matrix

print(arr[2,,2]) # prints 2nd row in second matrix

print(arr[3,3,1]) # prints 3rd row 3rd column element of 1st matrix

Matrix

Matrix is a two-dimensional rectangular data set. Elements of a matric will be of same atomic type.

matrix(data, rowSize, columnSize, byrow, dimnames)
  • data : data is an input vector
  • rowSize : defines no of row elements array can store
  • columnSize : defines no of column elements array can store
  • byrow : If it is set to TRUE then the input vector elements are arranged by row.
  • dimNames : specifies row and column names

Factors

Factor is a data object which is used to take a limited number of different values and categorize them into multiple levels.

factorData<- factor(inputVector) #`factor()` function is used to convert a vector into factor.
is.factor(factorData) # `is.factor()` is used to check whether the input given is a factor or not.

Data Frames

Data Frame is a data object which has like a 2D array like structure where column contains value of a variable and row contains one set of values from each column.

  • Column names should be non-empty.
  • Row names should be unique.
  • Data in a data frame can contain different types of data like a factor, numeric, or character type.
  • Every column will have same number of data items.

frame() function is used to create a data frame.

Example

student.data <- data.frame(
   studentID = c (101:104), 
   firstName = c("Foo","Bar","Alex","Mark"),
   marksPercentage = c(97.2,79.2,53.9,87.3), 
   
   joiningDate = as.Date(c("2010-06-01", "2010-06-10", "2010-06-04", "2010-06-02")),
   stringsAsFactors = FALSE
)
print(student.data)

conditional statements

1. If

if(conditional-expression)
{
    # code
}

2. If-else

if(conditional-expression)
{
    # code
} else {
    # code
}

3. Switch

switch(expression, case-1, case-2, case-3....)   

Loops

1. For

for (value in vector) {
  # code
}

2. While

while(condition){  
#code
}  

3. Repeat

repeat { 
   #code
   if(condition) {
      break
   }
}

Functions

Function is a sub-routine which contains set of statements.

function_name <- function(arg1, arg2, ...) { # defining a function
  #code 
}

function_name(arguments) # calling a function

Example

sum <- function(x, y) {
  cat("Sum:", x+y)
}

sum(10, 20)


┬ęCopyright 2020 OneCompiler | Privacy Policy | Terms & Conditions