Arman Akbarian
UNIVERSITY OF BRITISH COLUMBIA
PHYSICS & ASTRONOMY DEPT.

#------------------------------------------------------
# AAK: Sat  8 Aug 2014 22:35:40 PDT
# Hello World in R! Intro to commonly used objects in R
#------------------------------------------------------

# This is a comment

# Assignment 
a <- "Hello World!"
print(a)
# Which `type/class` is a:
b = class(a)
print(b)

#Numeric type
x <- 1.5
print(x)
print(class(x))

#Specifying integers:
j <- 2L
print(j)
print(class(j))

#Complex type:
a <- 2+3i
print(a)
print(class(a))


#Creating vectors:

x <- vector("numeric",length=5)
print(x)

#Creating quick integer vector:
j <- 2:7
print(j)

#Creating a sequence
x <-seq(1,10,0.2)
print(x)

#Concatination:
y <- c(1.3,2.7)
print(y)
print(class(y))


#R also has a logical type:
z <- c(TRUE, FALSE, TRUE, TRUE)
print(z)
print(class(z))

#Concatinating mixed objects: R performs conversion

#Converts numerics ==> character
w <- c("a",2.3)
print(w)

#Converts LOGICAL ==> 0(FALSE) or 1(TRUE)
m <- c(FALSE,3)
print(m)

#Convert LOGICAL ==> character
z <- c (TRUE,w)
print(z)

#Performing conversion using 'as' function

x <- -3:5
y <- as.logical(x)
print(y)

y <- as.complex(x)
print(y)

z <- as.character(x)
print(z)

#Invalid conversion returns NA:
z <- c("a","b")
d <- as.numeric(z)
print(d)

#Creating list which can contain mixed objects:
a <- list("a", TRUE, "b", 2+5i, 3L, 4.2)
print(a)

#Creating a matrix:
A <- matrix(nrow =2 , ncol=3)
print(A)
print(dim(A))

# Attributes of an object:
print(attributes(A))

# Matrix can be creating by adding the attribute dim to a vector
m <- 1:10
dim(m) <- c(2,5)
# Notice the ordering, first index runs first
print(m)

# Matrix can also be created by binding vectors:

x <- 1:4
y <- 11:14

# C-binding (Column)
m <- cbind(x,y)
print(m)

# R-binding (Row)
m <- rbind(x,y)
print(m)

# Accessing the element:
print(m[2,3])
# Accessing the entire column
print(m[,3])
# Accessing the entire row
print(m[1,])


# Factors: class of objects of n categories 
# where they are labeled properly (not integer levels) 
# thus better self-description
x <- factor(c("male" , "male" ,"female", "female","male"))
print(x)

# Creating a table:
print(table(x))

print("After unclassing a factor:")
y <- unclass(x)
print(y)

# Changing the base line level in factors (mapping of the categories to integers)
x <- factor( c("apple","apple","banana","orange","apple","banana","orange"),
             level=c("apple","orange","banana"))
print(table(x))

# Missing values, Not a number: NA, NaN

x <- c(1,2,4,NA,5,2/0,sqrt(-2))
b <- is.na(x)
c <- is.nan(x)
print(x)
print(b)
print(c)

# Data frames, matrix of entries with different types of columns:
x <- data.frame( foo = 10:13, bar=c("apple","apple","banana","orange"),
                isprime=c(FALSE,TRUE,FALSE,TRUE) )
print(x)

# Creating name attribute for objects (for creating self describing data)
x <- 1:3
names(x) <- c("foo","bar","baz")
print(x)

# Or equivalently it can be done using lists:
x <- list( a=1 , b= 2 , c=3)
print(x)

# Accessing elements of the object "subsetting"
x <- c("a" , "b", "c" , "a", "d")
# Using numeric index:
print(x[2])
print(x[1:3])

# Can get access to objects by logical condition:
print( x[x > "b"])
u <- x > "a"
print(u)
# Using logical indexing
print(x[u])

# Subsetting list

x <- list( foo=1:4, bar=1.2)

print(x[1])
print(x[[1]])
print(x[[1]][3])

print(x$bar)
print(x["bar"])
print(x[["bar"]])

# Note that this also works:
n <- "bar"
print(x[[n]])
# But this won't work, (it will literally look for object with name "n")
print(x$n)

# R has a sprintf!
dir <- "mydirectory"
num <- 12
fname <- sprintf("%s/%03d.csv",dir,num)
cat("fname is: ", fname , "\n")


last update: Wed Aug 19, 2015