In [30]:
getwd()
'/projects/fc96d042-48c2-4479-9884-fc9a89f4e931/Autumn2016/Week1'
In [32]:
setwd('~/Autumn2016')
In [35]:
objects()
'line'
In [1]:
x<-3
y<-10
z<-15

w<-(x+y+z)

return((y-x)/z)

x*y*z

sqrt(w)

w^4

a<-(z-x)
sqrt(a)
0.466666666666667
450
5.29150262212918
614656
3.46410161513775
In [59]:
myname <- "Joe"
Greeting <- "Hello my name is"
info <- "and I'm studying bioinformatics"
message <- paste(Greeting,myname,info,sep=" ")
print(message)
[1] "Hello my name is Joe and I'm studying bioinformatics"
55
10
In [1]:
x <- 1:10
In [80]:
assign('hello', 1:10)
print(hello)
sum(hello)
length(hello)
hello[5]
hello[3:8]
 [1]  1  2  3  4  5  6  7  8  9 10
55
10
5
  1. 3
  2. 4
  3. 5
  4. 6
  5. 7
  6. 8
In [54]:
assign('hello', 1:100)
print (hello)
sum(hello)
  [1]   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18
 [19]  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36
 [37]  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54
 [55]  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72
 [73]  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90
 [91]  91  92  93  94  95  96  97  98  99 100
5050
In [18]:
sample(1:100, 12, replace=TRUE)
runif(4, min=10, max=900)
rnorm(10, mean=100, sd=50)
  1. 85
  2. 91
  3. 98
  4. 78
  5. 70
  6. 19
  7. 22
  8. 38
  9. 15
  10. 100
  11. 69
  12. 86
  1. 798.076434270479
  2. 899.334471581969
  3. 327.451223421376
  4. 772.472544419579
  1. 80.8841282253673
  2. 53.5188803726913
  3. 135.198249783718
  4. 80.0738107649404
  5. 164.96422300186
  6. 130.775733930803
  7. 88.6591574989751
  8. 98.6583436276265
  9. 102.194391937866
  10. 103.928272084579
In [23]:
x <- seq(length=15,from=2,by=2)
print(x)
length(x)
sum(x)
y <- seq(length=15,from=1,by=2)
print(y)
sum(y)
z <- seq(length=30,from=1,by=1)
print(z)
sum(z)
#The sum value of z is almost double that of x and y.
 [1]  2  4  6  8 10 12 14 16 18 20 22 24 26 28 30
15
240
 [1]  1  3  5  7  9 11 13 15 17 19 21 23 25 27 29
225
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
[26] 26 27 28 29 30
465
In [24]:
greeting <- "how's it going"
number <- 78953728462
message <- c(greeting,as.character(number))
print(message)
[1] "how's it going" "78953728462"   
In [62]:
Courses <- c("BMS", "APS", "MBB")
Numbers <- c(353,227,253)
message <- c(Courses,as.character(Numbers))
print(message)

Modules <- Map(c,Courses,Numbers)
print(Modules)
[1] "BMS" "APS" "MBB" "353" "227" "253"
$BMS
[1] "BMS" "353"

$APS
[1] "APS" "227"

$MBB
[1] "MBB" "253"

In [17]:
A<-1:50
dim(A)<-c(10,5)
A

B<-1:100
dim(B)<-c(2,50)
B

X<-51:100
dim(X)<-c(2,25)
X
111213141
212223242
313233343
414243444
515253545
616263646
717273747
818283848
919293949
1020304050
1 3 5 7 9 11 13 15 17 19 81 83 85 87 89 91 93 95 97 99
2 4 6 8 10 12 14 16 18 20 82 84 86 88 90 92 94 96 98 100
51 53 55 57 59 61 63 65 67 69 81 83 85 87 89 91 93 95 97 99
52 54 56 58 60 62 64 66 68 70 82 84 86 88 90 92 94 96 98 100
Error in rbind(B, X): number of columns of matrices must match (see arg 2)
Traceback:

1. rbind(B, X)
In [10]:
matrix(1:40,nrow=4,ncol=10)
1 5 913172125293337
2 6 1014182226303438
3 7 1115192327313539
4 8 1216202428323640
In [44]:
a <-c(matrix(1:5,nrow=1,ncol=5))
b <-c(matrix(6:10,nrow=1,ncol=5))
rbind(a,b)
cbind(a,b)
a1 2 3 4 5
b6 7 8 9 10
ab
1 6
2 7
3 8
4 9
5 10
In [3]:
M <- matrix(1:20,nrow=4,ncol=5)
M

m <- matrix(1:4,nrow=2,ncol=2)

m[1,2] = 5
m[2,2] = 6

m
1 5 91317
2 6 101418
3 7 111519
4 8 121620
35
46
In [11]:
M <- matrix(1:20,nrow=4,ncol=5)
M

c <- matrix(1:8,nrow=2,ncol=4)

c[1,2] = 5
c[2,2] = 6

c[1,3] = 13
c[2,3] = 14

c[1,4] = 17
c[2,4] = 18

c
1 5 91317
2 6 101418
3 7 111519
4 8 121620
1 5 1317
2 6 1418
In [13]:
M <- matrix(1:20,nrow=4,ncol=5)
M

z <- matrix(1:5,nrow=1,ncol=5)

z[1,1] = 2
z[1,2] = 6
z[1,3] = 10
z[1,4] = 14
z[1,5] = 18

z
1 5 91317
2 6 101418
3 7 111519
4 8 121620
2 6 101418
In [37]:
x <- matrix(1:9,nrow=3,ncol=3)
rownames(x)<-c('2013-2014','2014-2015','2015-2016')
colnames(x)<-c('BMS353','APS227','MBB253')

x[1,1] = sample(200:350, 1, replace=TRUE) 
x[1,2] = sample(200:350, 1, replace=TRUE)
x[1,3] = sample(200:350, 1, replace=TRUE)
x[2,1] = sample(200:350, 1, replace=TRUE)
x[2,2] = sample(200:350, 1, replace=TRUE)
x[2,3] = sample(200:350, 1, replace=TRUE)
x[3,1] = sample(200:350, 1, replace=TRUE)
x[3,2] = sample(200:350, 1, replace=TRUE)
x[3,3] = sample(200:350, 1, replace=TRUE)

x

S = x[1,1]+x[2,1]+x[3,1]
S
BMS353APS227MBB253
2013-2014220290255
2014-2015282316229
2015-2016289294257
791
In [53]:
a <- matrix(nrow=3,ncol=4)
b <- matrix(nrow=3,ncol=4)

a[1,1] = runif(1, min=1, max=100)
a[1,2] = runif(1, min=1, max=100)
a[1,3] = runif(1, min=1, max=100)
a[1,4] = runif(1, min=1, max=100)
a[2,1] = runif(1, min=1, max=100)
a[2,2] = runif(1, min=1, max=100)
a[2,3] = runif(1, min=1, max=100)
a[2,4] = runif(1, min=1, max=100)
a[3,1] = runif(1, min=1, max=100)
a[3,2] = runif(1, min=1, max=100)
a[3,3] = runif(1, min=1, max=100)
a[3,4] = runif(1, min=1, max=100)

b[1,1] = runif(1, min=1, max=100)
b[1,2] = runif(1, min=1, max=100)
b[1,3] = runif(1, min=1, max=100)
b[1,4] = runif(1, min=1, max=100)
b[2,1] = runif(1, min=1, max=100)
b[2,2] = runif(1, min=1, max=100)
b[2,3] = runif(1, min=1, max=100)
b[2,4] = runif(1, min=1, max=100)
b[3,1] = runif(1, min=1, max=100)
b[3,2] = runif(1, min=1, max=100)
b[3,3] = runif(1, min=1, max=100)
b[3,4] = runif(1, min=1, max=100)

a
b

a+b
a-b
a*b
sqrt(a)
sqrt(b)
79.8389257.7886411.2538054.84656
69.3206859.6997410.5295498.08338
46.7278158.7598330.6721335.71172
75.1856022.4289521.5814864.05056
39.1114739.4546474.4341156.72634
18.8698344.7304811.1337840.71691
155.02452 80.2175932.83528 118.89712
108.43215 99.1543884.96364 154.80972
65.59764103.4903141.80591 76.42863
4.65332435.35968 -10.32768-9.204006
30.20921020.24509 -63.9045741.357034
27.85798314.02936 19.53835-5.005190
6002.73711296.139 242.8736 3512.953
2711.23362355.432 783.7567 5563.911
881.74592628.356 341.4969 1454.071
8.9352637.6018843.3546687.405846
8.3259047.7265603.2449259.903705
6.8357757.6654965.5382425.975929
8.6709634.7359224.6455878.003160
6.2539166.2812938.6275207.531689
4.3439426.6880853.3367336.380981
In [72]:
myfunction <-function(x) {
    result <- var(x)
    return (result)
}

test<-myfunction(runif(12,min=100,max=200))
test
951.046695142479
In [71]:
x<-runif(12,min=30,max=40)
x 
var(x)
  1. 38.5097428620793
  2. 37.8460048581474
  3. 37.1458997158334
  4. 30.0948898890056
  5. 31.5879360120744
  6. 34.5735970232636
  7. 38.4035057784058
  8. 34.8107304493897
  9. 38.0621735192835
  10. 32.9206168651581
  11. 37.5091786822304
  12. 34.0149591094814
8.27199033970175
In [104]:
x <-c(5,4,3,2,1)
n <- length(x)
mx <- mean(x)

sum((((mx-5)^2)+((mx-4)^2)+((mx-3)^2)+((mx-2)^2)+((mx-1)^2))/(n-1))
2.5
In [135]:
myfunction <- function(answer, argument2){
    argument2 <- var(x)
    answer <-c(mean(x),sd(x))
    return (answer)
}

test<-myfunction(runif(54,min=100,max=578))
test
  1. 163.371896767834
  2. 77.1297505092774

Things Covered in This Week's Practical

  • Creating vectors.
  • Creating matrices.
  • Carrying out functions on vectors and matrices.
  • Binding matrices and vectors.
  • Calculating variance using the formula provided and the var() function.
  • Using myfunction to create user-defined functions.
In [ ]: