2017-11-05 21_04_15-RStudio.png

Inside the R Studio

Copy and paste the following code to your R Studio platform or R version:

# ————————- Introductory Code in R ————————– #
print(‘Hello World!’)
5 / 2 * 3 # observe the order of the operations
10 %% 3 # the remainder
10 %/% 3 # the quotient
?print
??print

getwd()

install.packages(“devtools”)
library(devtools)

# Working with vectors
a <- “abc”
a
A # since R is case sensitive

class(a)
a <- 13
class(a)

a <- as.character(a)
class(a)

# Other types are integers, logicals and factors
# Data structures are a bit different. In R, we have vectors,
# matrices, data frames and lists. Vectors and matrices are
# homogeneous, whereas data frames and lists are heterogeneous

# Initializing a numeric vector
a <- numeric(10)
a

# Initializing a character vector
b <- character(10)
b

a[10] <- 10
a[1] <- 1
a
# Speeding up this assignment takes the following form:

a <- c(1,2,3,4,5,6,7,8,9,10)
a
# Even faster
a <- 10:20
a
class(a)

# Other ways of creating a vector are the following:
c <- rep(1,5)
c
d <- rep(c(1,2,3), 3)
d

e <- seq(1, 10, by = 2)
e

f <- seq(1, 10, length = 20)
f

g <- seq(1, by = 2, length = 10)
g

g[5]

# Everything except the 5th number
g[-5]

# If you want multiple values from the vector
g[c(5,6)]

# Factors
fac <- factor(c(“red”, “blue”, “white”, “red”, “blue”))
fac
class(fac)
levels(fac)
levels(fac) <- c(“purple”, “black”, “orange”)
fac

length(fac)
length(a)
a
names(a) <- c(“Ten”, “Eleven”, “Twelve”, “Thirteen”, “Fourteen”, “Fifteen”,
“Sixteen”, “Seventeen”, “Eighteen”, “Nineteen”, “Twenty”)
a
names(a)

# Another example
# Try grabbing the first and second to last item from the d vector

a <- 1:10
d <- c(a, a * 2, a * 3)
d
length(d)

# First solution
d[1]
d[length(d)-1]
# Also, consolidating:
d[c(1, length(d)-1)]

# Random Number, Rounding and Binning
set.seed(10)
# for generating the same random numbers everytime we run the code
a <- runif(10, 100, 150)
a

# Obtaining only the integer part of the values
b <- trunc(a)
b
b <- floor(a)
b

# Rounding to the nearest integer
c <- ceiling(a)
c

d <- round(a,1)
d

# Let us bin the vector
bins <- c(100, 110, 120, 130, 140, 150)
bins

d1 <- cut(d, bins)
d1

d2 <- pretty(d, 5) # it produces the bins variable that we have created manually
d2

# Let’s combine the methods
d3 <- cut(d, pretty(d,5))
d3

set.seed(100)
rNumbers <- runif(25, 1, 10)
rNumbers

rNumbers <- trunc(rNumbers)
rNumbers

# Dealing with missing values
a <- 21:30
a

a[9] <- NA
a

anyNA(a)
is.na(a)
# a == NA does not work

!is.na(a)
a[!is.na(a)]

# Replacing the NA with the mean
a[is.na(a)] <- mean(a, na.rm = TRUE)
a

# The which operator
set.seed(100)
a <- round(runif(25, 1, 100))
a

# Finding which numbers are greater than 25
pos <- which(a > 25)
pos
a[pos]

# Finding which numbers are multiples of 3 and 4
pos <- which(a%%3 == 0 & a%%4 == 0)
pos
a[pos] # only 36 is a multiple of both

# Let’s relax the condition a bit
pos <- which(a%%3 == 0 | a%%4 == 0)
pos
a[pos]

# The above examples will work without the which function as well
a[a%%3 == 0 & a%%4 == 0]
a[a%%3 == 0 | a%%4 == 0]
# In this case, instead of position, we are passing a logical vector,
# indicating which values to include:
a%%3 == 0 & a%%4 == 0

# Now, getting the even numbers from the vector a seems quite intuitive
a[a%%2 == 0]

Posted by Dimitris

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s