Skip to content

Instantly share code, notes, and snippets.

@furqon
Last active June 18, 2021 01:18
Show Gist options
  • Save furqon/90f54d0a4c4dee2b23efd9001d900c93 to your computer and use it in GitHub Desktop.
Save furqon/90f54d0a4c4dee2b23efd9001d900c93 to your computer and use it in GitHub Desktop.
Learning R

Learn R

course from GreatLearning

Introduction

types

  • numeric (ex: 1, 2, 3.4 regardless of float and integer)
  • character (string with one quote (') or two ("))
  • logical (TRUE / T and FALSE / F, if combine in numeric will be T = 1, F = 0)
  • complex (numeric with imaginary factor ex 13+6i)

get the type of var class(variable)

operators

  • assignment = equal to operator variable <- value_to_be_assigned or value_to_assigned -> variable
  • arithmetic + - * /
  • relational > greater than, <, ==, !=
  • logical & and, | or

vector linear homogenous data structure

  • using combiner vec <- c(1,2,3,4,5)
  • homogenous mean cannot be different type (ex: numeric and character
  • if theres character than all variable converted into char, with order char > numeric > logical
  • vec[2] get the second value of variable vec
  • vec[1:3] get the value of vec from 1 to 3
  • vec[c(1,3)] get the value of vec on index 1 and 3

list more like vector, but not homogenous (can have multiple type (num, char, logical, vector). l1 <- list(a, 'char', T) class(l1[[1]]) get the type of list index 1

matrix m1 <- matrix(c(1,2,3,4,5,6), nrow(2), ncol(3), byrow = T)

  • nrow is to spread value in number of row
  • ncol is to spread value in number of col
  • byrow is to place the value by T: row first, F: col first
  • to extract the value inside matrix m1[row, col] if row is empty than will extract for all row m1[, col] vice versa

array multi dimensional homogenous data structure (multi vector) example

vec1 <- c(1,2,3,4,5,6) vec2 <- c(7,8,9,10,11,12) #a1 <- array(c(vec1, vec2), dim=(row, col, numOfElementInArray) a1 <- array(c(vec1, vec2), dim=(2, 3, 2) extracting value from array, like getting from dim params a1[row, col, arrayIndex] read by taking

  1. extract from array number index
  2. get row from that array
  3. get col from that array

factor and dataframe

  • factor: to get the distinct value of a vector
  • dataframe: 2 dimensional heterogenous data structure, simply like table in excel tblx <- data.frame(ColName1=c("v1","v2","v3"), ColName2=c("u1","u2","u3"))
  • to get the value of col1 tblx$ColName1
  • to view in table wise view(tblx), better to not do viewing on large dataset, will consume memory
  • we can use tibble package for better viewing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment