#Assignment: the "<-" operator a<-1 a #Typed vectors x<-c(1,2,3) y<-5:7 z<-seq(0,50,1) u<-rep(11,5) #Vector manipulation x+3 y/2 z*2 x+y u^2 y%%5 #this gives the integer remainder after dividing by 5 #Question: Find the first 23 multiples of the number 7 and #assign this to a variable called mul7 #---------------------------------------------------------- #---------------------------------------------------------- #Isolating parts of vectors #remember we had y<-5:7 from before y[1] y[2] y[3] y[1:2] y[c(1,3)] y[c(FALSE,TRUE,TRUE)] #Question: Find the 18th multiple of 7 #-------------------------------------- #-------------------------------------- #Logic operations #remember we had a<-1 a==1 a==2 a!=3 a>=1 a<0 #Question: Test whether a is strictly less than -1 #------------------------------------------------- #------------------------------------------------- b<-2 a==1 & b==2 a==1 | b==1 #Question: Find out which multiples of 7 are also multiples of 4 #--------------------------------------------------------------- #--------------------------------------------------------------- #Conditional and loop statements: #remember we had a<-1 if(a==2) { q<-2 } else { q<-1 } q v<-rep(0,10) for(i in 1:10) { v[i]<-i } v # Question: Using a "for" loop, construct a vector of perfect squares x^2 for x from 1 to 10 #----------------------------------------------------------------------------------------- #----------------------------------------------------------------------------------------- #Writing simple functions: MySum.f<-function(a,b) { return(a+b) } MySum.f(2,3) MySum.f(x,y) MyQuadForm.f<-function(a,b,c) { sol<-rep(0,2) sol[1]<-((-b+sqrt(b^2-4*a*c))/(2*a)) sol[2]<-((-b-sqrt(b^2-4*a*c))/(2*a)) return(sol) } MyQuadForm.f(1,1,-1) # Question: Write a function to give the nth number in the Fibonacci sequence: # 1 1 2 3 5 8 13 21 ... #--------------------------------------------------------------------------------- #--------------------------------------------------------------------------------- #Tools for stochasticity: #Take a random sample from a vector without replacement sample(x,size=3,replace=FALSE) #Take a random sample from a vector with replacement sample(y,size=10,replace=TRUE) #Question: Write a function that takes a bitstring (a vector #of 0's and 1's) and returns a new bitstring with a specified #number of random mutations #---------------------------------------------------------- #---------------------------------------------------------- #Random uniformly distributed number: runif(n=1, min=0, max=1) #Random normally distributed number: rnorm(n=1, mean=10, sd=2) #Random Poisson distributed number: rpois(n=1, lambda=4) #Random binomially distributed number: rbinom(n=1, size=20, prob=0.5) #Question: Give the random number of offspring (out of 100) #with a recessive phenotype resulting from a cross #between two heterozygotes #---------------------------------------------------------- #----------------------------------------------------------