For Loops and If Statements
- Using a
for loop and an if statement,
write a function to calculate the number of zeroes in a numeric vector.
Before entering the loop, set up a vector of integers with some zero
values. Then, set up a counter variable counter <- 0. Inside the
loop, add 1 to counter each time you have a zero in the vector. Finally,
use return(counter) for the output.
vec<-(round(runif(n=50, min=0, max=1))) # create a vector of 50 rounded random uniform numbers between 0 and 1
print(vec)
## [1] 1 1 1 1 1 0 0 1 0 1 0 1 1 1 1 0 0 0 1 1 0 0 1 1 0 0 0 0 1 1 1 0 1 1 0 0 0 0
## [39] 0 0 0 1 0 0 0 0 0 1 0 1
vecfn<-function(vector){
counter<-0 # assign 0 to counter
for(i in 1:length(vector)){ # if the value of the 'ith' iteration is 0, add 1 to counter
if (vector[i]==0) {counter<-counter+1}
}
return(counter) # print the counter, which counted how many zeros were present
}
vecfn(vec) # run function with my vector
## [1] 27
- Write a function that takes as input two integers representing the
number of rows and columns in a matrix. The output is a matrix of these
dimensions in which each element is the product of the row number x the
column number.
intmate<-function(int1, int2){ # argument of fn is the placeholder for what the fn is going to take, this fn will take (int1, int2)
mate<-matrix(nrow=int1, ncol=int2) # create a matrix named "mate" where the number of rows is = int1 and columns = int2
for(i in 1:nrow(mate)){
for(j in 1:ncol(mate)){
mate[i,j] <- i*j # for the ith row and jth column of my matrix "mate", multiply the row number * column number. in other words, the value of [i,j] is i*j.
} # end of column j loop
}
return(mate) # print this matrix
}
# Let's try it out:
intmate(3,4)
## [,1] [,2] [,3] [,4]
## [1,] 1 2 3 4
## [2,] 2 4 6 8
## [3,] 3 6 9 12
- Use a for loop to repeat your previous functions from Weekly
Assignment 2 (which simulate a data frame and extract a p-value) 100
times. Store the results in a data frame that has 1 column indicating
the replicate number and 1 column for the p-values (a total of 2 columns
and 100 rows)
df_func<-function(means=c(10,45),sds=c(1.5,2)){
Type<-rep(c("Control", "Treatment"), each=25) # create first column with 25 lines of both treatment and control
Value<-c(rnorm(n=25, mean=means[1], sd=sds[1]),(rnorm(n=25, mean=means[2], sd=sds[2]))) # create second column with random values which also fit mean/sd criteria
typevalue_df<-data.frame(Type, Value, stringsAsFactors = FALSE) # create df with "Type" and "Value" columns
return(typevalue_df) # return df please
}
df_func()
## Type Value
## 1 Control 11.754205
## 2 Control 7.811502
## 3 Control 10.334977
## 4 Control 10.332325
## 5 Control 9.812150
## 6 Control 10.834477
## 7 Control 9.724351
## 8 Control 11.209358
## 9 Control 11.227937
## 10 Control 12.647590
## 11 Control 7.912117
## 12 Control 11.455070
## 13 Control 11.278742
## 14 Control 9.685053
## 15 Control 11.585512
## 16 Control 9.678881
## 17 Control 11.628725
## 18 Control 11.963833
## 19 Control 11.741913
## 20 Control 9.172467
## 21 Control 7.817881
## 22 Control 8.780888
## 23 Control 9.467445
## 24 Control 11.769507
## 25 Control 7.681951
## 26 Treatment 46.166363
## 27 Treatment 44.767956
## 28 Treatment 48.913806
## 29 Treatment 44.594744
## 30 Treatment 46.648383
## 31 Treatment 43.336227
## 32 Treatment 47.165618
## 33 Treatment 43.880686
## 34 Treatment 42.266466
## 35 Treatment 45.214471
## 36 Treatment 43.550366
## 37 Treatment 43.240048
## 38 Treatment 46.805281
## 39 Treatment 46.342233
## 40 Treatment 45.402572
## 41 Treatment 47.087675
## 42 Treatment 49.079082
## 43 Treatment 45.201057
## 44 Treatment 43.594040
## 45 Treatment 44.455794
## 46 Treatment 48.173458
## 47 Treatment 45.065747
## 48 Treatment 45.779263
## 49 Treatment 43.428095
## 50 Treatment 46.802873
q1df<-df_func()
ANOVA_Test<-aov(Value ~ Type, data=q1df)
pvalue_func<-function(data=NULL){
p<-summary(ANOVA_Test)[[1]][["Pr(>F)"]][1]
return(p)
}
pval<-pvalue_func(data=ANOVA_Test) # assign the name pval to the pvalue returned from my ANOVA_Test
newdf<-data.frame(x=1:100, y=NA) # create a new df where the first row is the replicate number and the second row is NA's which will be filled with p values following the for loop.
for (i in 1:100){
q1df<-df_func() # run this function df_func() and assign it to the name 'q1df'
ANOVA_Test<-aov(Value ~ Type, data=q1df) # run an ANOVA based on my q1df data
pval<-pvalue_func(data=ANOVA_Test) # run the function that finds the pvalue of the ANOVA data and assign this to 'pval'
newdf[i,2] <- pval # in my newdf, place pval in the ith row, 2nd column (which previously was filled with NA's)
}
print(newdf)
## x y
## 1 1 1.782081e-52
## 2 2 3.357211e-46
## 3 3 6.572586e-53
## 4 4 6.312704e-52
## 5 5 8.032815e-49
## 6 6 5.348627e-52
## 7 7 1.154582e-49
## 8 8 1.017143e-48
## 9 9 1.030922e-50
## 10 10 8.407049e-51
## 11 11 2.807252e-51
## 12 12 2.334892e-51
## 13 13 1.934957e-54
## 14 14 8.008012e-51
## 15 15 5.326602e-52
## 16 16 7.360820e-48
## 17 17 2.736269e-53
## 18 18 1.782552e-50
## 19 19 3.309446e-56
## 20 20 3.235137e-51
## 21 21 1.464995e-47
## 22 22 7.562337e-52
## 23 23 2.047105e-49
## 24 24 4.480968e-49
## 25 25 7.677387e-53
## 26 26 4.028395e-48
## 27 27 1.722558e-50
## 28 28 2.524193e-49
## 29 29 1.970511e-49
## 30 30 1.383643e-50
## 31 31 1.198036e-52
## 32 32 1.927377e-48
## 33 33 8.958382e-53
## 34 34 3.160898e-49
## 35 35 9.772467e-49
## 36 36 6.234822e-52
## 37 37 1.468622e-48
## 38 38 9.676964e-46
## 39 39 1.239001e-52
## 40 40 2.811649e-47
## 41 41 4.499068e-47
## 42 42 1.847507e-52
## 43 43 1.151166e-51
## 44 44 1.044711e-48
## 45 45 4.451606e-50
## 46 46 1.289827e-50
## 47 47 1.209629e-51
## 48 48 3.359060e-50
## 49 49 5.968687e-49
## 50 50 6.022259e-49
## 51 51 2.449708e-47
## 52 52 2.084482e-53
## 53 53 1.014371e-48
## 54 54 5.093343e-50
## 55 55 1.233959e-49
## 56 56 2.015668e-48
## 57 57 3.064295e-48
## 58 58 2.886313e-48
## 59 59 8.404702e-55
## 60 60 1.061862e-47
## 61 61 1.338889e-48
## 62 62 1.796224e-46
## 63 63 6.382850e-47
## 64 64 4.573198e-49
## 65 65 2.275589e-49
## 66 66 3.807363e-49
## 67 67 2.063752e-54
## 68 68 6.133436e-50
## 69 69 2.663031e-51
## 70 70 4.625843e-54
## 71 71 2.025303e-48
## 72 72 9.112913e-49
## 73 73 1.999268e-48
## 74 74 5.420352e-50
## 75 75 2.860549e-48
## 76 76 4.151040e-53
## 77 77 2.757620e-50
## 78 78 3.489610e-50
## 79 79 6.513822e-49
## 80 80 9.997496e-53
## 81 81 5.081333e-51
## 82 82 2.601577e-48
## 83 83 1.272695e-49
## 84 84 7.223176e-47
## 85 85 6.519679e-49
## 86 86 5.999393e-49
## 87 87 2.419908e-49
## 88 88 3.587501e-48
## 89 89 3.580738e-49
## 90 90 1.084367e-47
## 91 91 1.125004e-48
## 92 92 4.074579e-52
## 93 93 1.579765e-47
## 94 94 3.703953e-51
## 95 95 1.535653e-52
## 96 96 3.044492e-52
## 97 97 7.254522e-50
## 98 98 1.283364e-52
## 99 99 6.663784e-52
## 100 100 2.442938e-46