Skip to content

Instantly share code, notes, and snippets.

@30atm
Last active September 23, 2017 10:49
Show Gist options
  • Save 30atm/de337607678c6c2ae31b48ef603e5aad to your computer and use it in GitHub Desktop.
Save 30atm/de337607678c6c2ae31b48ef603e5aad to your computer and use it in GitHub Desktop.
R

Homogeneous Collections

  • Atomic Vector (1 dimension)
  • Matrix (2 dimension)
  • Array (n dimension)

Heterogeneous Collections

  • List (1 dimension)
  • Data frame (2 dimension)
  • Factor

Vector examples

student.names <- c( "ram" ,"krishna", "kumar", "ganesh")

student.weights <- c (60.6, 70.5, 80.4, 68.5)

student.physicsmarks <- (50L, 60L, 70L, 80L) #Suffix L is needed to make it a integer vector instead of numeric(decimal) vector

student.likesPhysics <- (F, FALSE, TRUE, T) # T /TRUE indicate true

str(stduent.names) # returns the structure of a vector

is.character(student.names)

is.numeric(student.marks)

creating a vector

vector("character",length=50)

Arithmetic and logical operations on vectors

student.marks >= 75

student.mars +

print (student.names[1])

Extracting sub lists

student.names[1:3]

as.numeric(student.marks) converts ints to floats

as.integer(student.weights) conversts floats to ints

?range #exact help search
??av  #pattern help search

help("range") # is same as ?range


demo()
demo(package = .packages(all.available = TRUE))
deom(package = 'graphics')
demo('graphics')

vignette()  #Built in pdf based help
vignette(package= .packages(all.available = TRUE))
vignette(package= 'parallel')
vignett('parallel', package='parallel')


#searching in http://search.r-project.org
RsiteSearch("arithmetic mean")

RsiteSearch ("{arithmetic mean}") # exact match search

#prettified help library
install.packages("sos")
library (sos) #load sos
findFn("{arithmetic mean}")
findFn("{arithmetic mean}", maxPages=2)
???"{arithmetic mean}"(2) # shortcut for findFn


Use [R] while searching in google instead of simply R

> print("hello world")
[1] "hello world"
> mean(1:20)
[1] 10.5
> mean ( 1 21)
Error: unexpected numeric constant in "mean ( 1 21"
> mean(1:21:2)
[1] 1.5
Warning message:
In 1:21:2 : numerical expression has 21 elements: only the first used
> mean(1:21,2)
[1] 11
> mean(1:21,3)
[1] 11
> range(1:100,4)
[1]   1 100
> print(range (1:50,5))
[1]  1 50
> x <- 1:20
> print(x)
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20
 
 source("c:/projects/R/a01.sample.R",echo=TRUE)

Notes on learning R

#by convention, _ is not recommended in variable name. Use camelCase or "." as a separator

match.score <- 300

assign("match.score", 300) # Both the above are same.

Environments

Environments are namespaces for containing variables.

# create a new environment (a.k.a, namespace)
my.env <- new.env()

# create a variable in a custom environment
# All the below syntaxes are equally valid
assign ("x", 10, my.env)
my.env [[ "x "]] <- 10
my.env$x <-10

print (get("x", my.env))

print (my.env$x)

#getting reference to global environment
globalenv()

Operators


+  "+"
+ "-"
+ /
+ "\*"
+ ^ or \*\*  (exponentiation)
+ %% (modulo)
+ %/% (integer division)

Basic Math functions
  • abs(-5)
  • log(5)
  • exp(5)
  • factorial (5)

Special constants


+ pi

Global options
~~~~~~~~~~~~~~

+ options() 
+ options(global =10) # set number of digits

Special Numbers
~~~~~~~~~~~~~~~

+ Infininty ( **-Inf , Inf**)
+ Not a number (**NaN**)
+ Not available (**NA**)

Functions to check special numbers
  • is.finite(1/0)
  • is.infinite( 1/0)
  • is.nan( Inf/Inf)
  • is.na (NA)
  • is.na (NaN)

Vectorized operations


This is similar to map operation on a list

A vector is a one dimensional set of values of similar type.

Combinig a number of values into a vector

student.marks <- c (10, 20, 30 , 40)

student.marks <- student.marks + 5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment