Purpose: to use basic commands, create objects, use functions, and plot simple statistics objects # Basic commands The assingment operator (<-) assigns a value to a variable (x) Type the variable name to print the contents of the variable

x <- c(1, 2, 3, 4, 5)
x
## [1] 1 2 3 4 5

Using functions on variables

The length() function returns the number of values stored in a given variable Using operators (+ = * /) on a vector will act on the corresponding components i.e. x + y = (x1 + y1, x2 + y2,…)

y <- c(6, 7, 8, 9, 10)
length(x)
## [1] 5
length(y)
## [1] 5
x + y
## [1]  7  9 11 13 15

General variable list commands

ls() lists the objects in the environment rm() removes only specified objects from the environment rm(list = ls()) removes ALL ojects from the environment

ls()
## [1] "x" "y"
rm(x, y)
ls()
## character(0)
x <- c(1, 2, 3, 4, 5)
y <- c(6, 7, 8, 9, 10)
rm(list = ls())

Matrix Operations

?object will look up the specified command/object in the r documentation Labels within the argument of the function are not needed but can make code more readable byrow specifies how the matrix is filled in, the default is false so the matrix fills by columns but if true the matrix will fill rows before columns Operations done on a matrix will act independently on each cell

x <- matrix(data = c(1,2,3,4), nrow=2, ncol=2)
x
##      [,1] [,2]
## [1,]    1    3
## [2,]    2    4
y <- matrix(c(1,2,3,4), 2, 2)
z <- matrix(c(1,2,3,4),2,2, byrow=TRUE)
z
##      [,1] [,2]
## [1,]    1    2
## [2,]    3    4
sqrt(x)
##          [,1]     [,2]
## [1,] 1.000000 1.732051
## [2,] 1.414214 2.000000
x^2
##      [,1] [,2]
## [1,]    1    9
## [2,]    4   16

Using rnorm to generate random numbers

rnorm(50,mean=50,sd=.1) generates a list of 50 random numbers with a mean of 50 and a standard deviation of 0.1 By setting the seed to a certain value each time rnorm() is used, the generated values are reproducible

rm(list = ls())
x <- rnorm(50)
x
##  [1] -0.58337102  0.40297289 -0.06541457 -0.16780399  1.26441185 -0.41621360
##  [7] -0.07070103 -0.72582289  0.75912549  0.25441223  1.29768389 -0.86694463
## [13]  1.38135545 -1.73463321 -2.82804214 -0.55163199 -0.74952769  0.34667494
## [19] -0.74885214 -1.63322929 -0.07454824  0.60597737  0.20391554 -0.33565404
## [25] -0.84449619 -1.53244880 -0.22214326 -0.04750947  1.57027264 -1.54155719
## [31]  0.48703291 -0.68923422 -0.35343849  0.51810559  1.17243531 -1.34519645
## [37]  0.71440578  1.69997620  0.86004475 -0.62401576  0.80418466 -0.20696480
## [43]  0.41007789 -0.10025896  1.76567496 -0.23456518  1.51253204  0.79560386
## [49]  0.37400096 -1.53341303
y <- x + rnorm(50, mean = 50, sd = 0.1)
y
##  [1] 49.57708 50.29830 49.97642 49.78530 51.15969 49.60111 49.95487 49.34005
##  [9] 50.80585 50.22664 51.19871 49.11276 51.38351 48.34467 47.03657 49.54402
## [17] 49.20012 50.25040 49.08775 48.10032 50.08862 50.57336 50.25428 49.60298
## [25] 49.10558 48.35475 49.75369 49.82577 51.69536 48.46908 50.60575 49.22183
## [33] 49.62082 50.58308 51.26715 48.58083 50.73764 51.65121 50.73086 49.39063
## [41] 50.72627 49.80476 50.40405 49.88470 51.75258 49.68056 51.50380 50.85181
## [49] 50.31431 48.41516
cor(x,y)
## [1] 0.996683
set.seed(1303)
rnorm(50)
##  [1] -1.1439763145  1.3421293656  2.1853904757  0.5363925179  0.0631929665
##  [6]  0.5022344825 -0.0004167247  0.5658198405 -0.5725226890 -1.1102250073
## [11] -0.0486871234 -0.6956562176  0.8289174803  0.2066528551 -0.2356745091
## [16] -0.5563104914 -0.3647543571  0.8623550343 -0.6307715354  0.3136021252
## [21] -0.9314953177  0.8238676185  0.5233707021  0.7069214120  0.4202043256
## [26] -0.2690521547 -1.5103172999 -0.6902124766 -0.1434719524 -1.0135274099
## [31]  1.5732737361  0.0127465055  0.8726470499  0.4220661905 -0.0188157917
## [36]  2.6157489689 -0.6931401748 -0.2663217810 -0.7206364412  1.3677342065
## [41]  0.2640073322  0.6321868074 -1.3306509858  0.0268888182  1.0406363208
## [46]  1.3120237985 -0.0300020767 -0.2500257125  0.0234144857  1.6598706557
set.seed(6)
y <- rnorm(100)
y
##   [1]  0.26960598 -0.62998541  0.86865983  1.72719552  0.02418764  0.36802518
##   [7] -1.30920430  0.73862193  0.04487299 -1.04839720  1.72785109 -1.17859974
##  [13]  0.65320671 -0.36856649 -0.59955464  0.05460517  1.70767743 -1.09437298
##  [19] -0.28928182  2.20741296  0.51874901 -1.40491794  2.01486448 -1.18815834
##  [25]  0.19038081 -1.16973591 -0.03808156  2.35420426  1.39342626 -0.56033236
##  [31] -0.67145938  0.49243855 -1.17939052 -1.05871745  1.13790261 -0.16026528
##  [37]  0.63049313  1.61695970 -0.19349983 -1.60779184 -0.88516413 -0.43233430
##  [43] -0.42162386 -0.17049406  0.24581094 -0.74574778 -0.27394413  1.82457894
##  [49]  0.01423374  0.18804274 -0.05413369  0.46161167 -0.59677030  1.26325824
##  [55] -1.14532904  1.08462356 -1.52899531 -1.57374188 -0.11409263  0.11120918
##  [61]  0.21413762  0.55377583 -1.05951952 -1.61004933 -0.33815106  0.20493775
##  [67] -0.22434504 -0.90960970 -0.80810988  0.55308351 -0.38917793 -0.44724564
##  [73] -0.02113885 -0.59941746 -0.31086619 -0.68163247 -0.20205572  1.11680033
##  [79]  0.82599921  1.25091898  2.60809809 -0.05103996  2.22719419 -0.01387212
##  [85] -1.54739970 -1.37988911  1.47987074 -0.25492194 -0.32628038 -0.72663867
##  [91] -1.95234865  0.42294004  1.18168479  0.91795938  0.09546755 -1.68443179
##  [97]  0.99032935 -0.70783178 -0.59402917 -1.06589703

Descriptive statistics

mean(x)
## [1] -0.0325351
var(x)
## [1] 1.015221
sqrt(var(x))
## [1] 1.007582
sd(x)
## [1] 1.007582

Base R Graphics

rm(list = ls())
x = rnorm(100)
y = rnorm(100)
plot(x,y)

plot(x, y, xlab = "X-axis", ylab = "Y-axis", main = "Plot of X vs Y")

pdf("Lab01 Figure.pdf")
plot(x, y , col = "purple")
dev.off()
## quartz_off_screen 
##                 2

Generating vector of sequential numbers

All three methods create the same vector

x <- seq(1,10)
y <- c(1:10)
z <- 1:10
x
##  [1]  1  2  3  4  5  6  7  8  9 10
y
##  [1]  1  2  3  4  5  6  7  8  9 10
z
##  [1]  1  2  3  4  5  6  7  8  9 10

Fancy graphs

rm(list = ls())
x <- seq(-pi, pi, length = 50)
y <- x
f <- outer(x,y,function(x,y)cos(y)/(1+x^2))
contour(x,y,f)
contour(x,y,f,nlevels=45,add=T)

fa=(f-t(f))/2
contour(x,y,fa,nlevels=15)

image(x,y,fa)

persp(x,y,fa)

persp(x,y,fa,theta=30)

persp(x,y,fa,theta=30,phi=20)

persp(x,y,fa,theta=30,phi=70)

persp(x,y,fa,theta=30,phi=40)