Skip to content

Commit

Permalink
Lecture 1
Browse files Browse the repository at this point in the history
It covers the basics of R, plots, matrices, arrays, lists and some
about data frames
  • Loading branch information
srodrb committed Sep 22, 2014
1 parent 665db92 commit a0e3f6d
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 0 deletions.
32 changes: 32 additions & 0 deletions Lecture 1 /Lecture1_introduction.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# The script works on the current directory
#
# Samuel Rodriguez Bernabeu
# Assignment: lecture 1
# deliver date: 22/09/14
#
#

rm(list=ls()) # clear the actual workspace

# we create the elasticband dataframe
elasticband <- data.frame( stretch = c(46,54,48,50,44,42,52), distance = c(148,182,173,166,109,141,166))

# exercise 1: create summary statistics for the elastic band data
summary( elasticband )

# exercise 2: create a plot of distance versus stretch
plot( distance ~ stretch , elasticband, main="Distance vs. stretch", xlab="Stretch [N]", ylab="Distance [mm]")

# exercise 3: use help() command to find more information about the hist() command
help( hist )

# exercise 4: create a histogram of the distance using hist()
hist( elasticband$distance, main="Histogram of distance", xlab="Distance [mm]", ylab="Frequency")

# exercise 5: create the dataframe of snow cover data and plot the results
snow <- data.frame( year=1970:1979, snow.cover=c(6.5,12.0,14.9,10.0,10.7,7.9,21.9,12.5,14.5,9.2))
plot( log(snow.cover) ~ year, snow, main="Snow cover for Eurasia in the years 1970-1979", ylab="log of snow cover", xlab="Year")

# exercise 6: remove the elasticband dataframe
rm( elasticband)

26 changes: 26 additions & 0 deletions Lecture 1 /Lecture1_lists.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# The script works on the current directory
#
# Samuel Rodriguez Bernabeu
# Assignment: lecture 1
# deliver date: 22/09/14
#
#

rm(list=ls()) # clear the actual workspace

# Exercise 1: create the vector containing the provided data
Year <- c(1980,1988,1996,1998,2000,2002)
mean_weight <- c(71.5,72.1,73.3,74.3,75.2,74.7)
Gender <- c("M", "M", "F", "F", "M", "M")
mean_height <- c(179.3,179.9,180.5,180.1,180.3,180.4)

# Exercise 2: Create a list called mylist containing the above vector.
# Give each component of the list a name

mylist <- list( Year=Year, Weight=mean_weight, Gender=Gender, Height=mean_height)
mylist

# Exercise 3: use three different ways to access the 4th element of the list
mylist[4] # accessing via indexing directly to the 4th element.
mylist["Height"] # "Height" is the name of the fourth element of the list
mylist$Height # "Height" is the name of the fourth element of the list
51 changes: 51 additions & 0 deletions Lecture 1 /Lecture1_matrices_and_arrays.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# The script works on the current directory
#
# Samuel Rodriguez Bernabeu
# Assignment: lecture 1
# deliver date: 22/09/14
#
#

rm(list=ls()) # clear the actual workspace

# Exercise 1: construct a matrix A with values 10,20,30,50 in column 1 (...)
array <- c(10,20,30,50,1,4,2,3,15,11,19,5)
A <- matrix( array, nrow=4, ncol=3)
A

B = matrix( data=c(2.5,3.5,1.75), nrow=1, ncol=3)
B


# Exercise 2: combine A and B into a new matrix C using cbind()
C = cbind( A, B)
C
# cbind(A,B) throws an error, "the number of rows on the matrices must be the same"


# Exercise 3: combine A and B into a new matrix C using rbind()
H = rbind(A,B)
H

# Exercise 4: determine the dimensions of C and H using dim() function
dim(H)


# Exercise 5: calculate the following
M = matrix( data=c(1,4,3,0,-2,8), nrow=2, ncol=3)
N = matrix( data=c(1,9,2,17,-6,3), nrow=3, ncol=2)
result <- M%*%N
result

# Exercise 6: create a 4x4x2 array arr using the values 1 to 32
arr <- array( data=1:32, c(4,4,2))

# Exercise 7: print out the value in row 1, column 3 of the first 'matrix'
arr[1,3,1]


# Exercise 8: print out the value in row 2, column 4 of the second 'matrix'
arr[2,4,2]

# Exercise 9: add these two values together
arr[1,3,1] + arr[2,4,2]
37 changes: 37 additions & 0 deletions Lecture 1 /Lecture1_vector_and_factors.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# The script works on the current directory
#
# Samuel Rodriguez Bernabeu
# Assignment: lecture 1
# deliver date: 22/09/14
#
#

rm(list=ls()) # clear the actual workspace

# Exercise 1: create a vector x with the following entries, then change the 1's to 0's
x <- c(3,4,1,1,2,1,4,2,1,1,5,3,1,1,1,2,4,5,5,3)
x[ (x==1) ] = 0

# Exercise 2: create a vector y containing the elements of x that are greater than 1
y<-x[x>1]

# Exercise 3: create a sequence of numbers from 1 to 20 in steps of 0.2 and store
z<-seq( 1, 20, 0.2)

# Exercise 4: concatenate x and y into a vector called newVec
newVec <- c(x,y)

# Exercise 5: display all objects in the workspace and then remove newVec
ls()
rm(newVec)

# Exercise 6: six patients were asked to rate their pain from 0 to 3, with 0 representing "no pain", 1 representing 'mild' pain,
# 2 representing 'medium' pain and 3 'severe' pain.
# create a factor fpain to represent the data.

# Explanation: I've done this using a dataframe instead of a single vector. For a single vector would be the same

pain <- data.frame( Patient=1:6, fpain=c(0,3,1,2,1,2))
pain$fpain = factor( pain$fpain, levels=0:3)
levels(pain$fpain) <- c("no pain", "mild", "medium", "severe")
summary(pain)

0 comments on commit a0e3f6d

Please sign in to comment.