Using a for loop, write a function to calculate the number of zeroes in a numeric vector. Before entering the loop, 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.
source("Homework_09Functions.R")
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
## Warning: `data_frame()` was deprecated in tibble 1.1.0.
## ℹ Please use `tibble()` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
x<- rep(1:10, times=25)
rows<-5
columns<-4
zero_counter<-function(vec){
counter<-0
for (i in vec){
if (i == 0){
counter<- counter+1
}
}
return(counter)
}
print(zero_counter(vec=x))
## [1] 0
Use subsetting instead of a loop to rewrite the function as a single line of code.
SubsetOfZero<-function(vec){
return(sum(vec == 0))
}
print(SubsetOfZero(vec=x))
## [1] 0
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.
EasyMatrix<-function(rows=x,columns=y){
m<-matrix(nrow=rows,ncol=columns)
for(i in 1:nrow(m)) {
for(j in 1:ncol(m)) {
m[i,j]<- i*j
}
}
return(m)
}
EasyMatrix(rows=7,columns=3)
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 2 4 6
## [3,] 3 6 9
## [4,] 4 8 12
## [5,] 5 10 15
## [6,] 6 12 18
## [7,] 7 14 21
Now let’s practice calling custom functions within a for loops. Use the code from previous lectures on loops and functions to complete the following steps:
Simulate a dataset with 3 groups of data, each group drawn from a distribution with a different mean. The final data frame should have 1 column for group and 1 column for the response variable
nName= c("Potatoes","Carrots","Radishes")
nSize= c(rep(20, times=3))
nMean= c(17,25, 19)
nSD= c(runif(3))
VeggieWeight<-FakeDataGeneration()
print(head(VeggieWeight))
## # A tibble: 6 × 3
## Index Treatment Value
## <int> <chr> <dbl>
## 1 1 Potatoes 17.0
## 2 2 Potatoes 17.1
## 3 3 Potatoes 17.2
## 4 4 Potatoes 16.9
## 5 5 Potatoes 16.7
## 6 6 Potatoes 17.5
Write a custom function that 1) reshuffles the response variable, and 2) calculates the mean of each group in the reshuffled data. Store the means in a vector of length 3.
Randomizer<-function(x){
CleanWeight<-mutate(x, Treatment,Value=round(Value, digits=1))
NewWeight<-sample(CleanWeight$Value)
MixedWeight<-mutate(CleanWeight, Treatment, NewWeight)
MixedWeight<-select(MixedWeight, Treatment, NewWeight)
Answer<-group_by(MixedWeight, Treatment) %>% summarize(m = mean(NewWeight))
return(unlist(Answer[,2]))
}
print(head(Randomizer(x=VeggieWeight)))
## m1 m2 m3
## 19.690 20.255 21.055
Use a for loop to repeat the function in b 100 times. Store the results in a data frame that has 1 column indicating the replicate number and 1 column for each new group mean, for a total of 4 columns.
FrameWork<- data.frame(matrix(nrow=100,ncol=3))
for (i in 1:nrow(FrameWork)) {
FrameWork[i,]<-Randomizer(x=VeggieWeight)
}
FrameWork<-mutate(FrameWork, ID=(1:100))
FrameWork<-select(FrameWork, ID,Potatoes=X1,Carrots=X2,Radishes=X3)
print(head(FrameWork))
## ID Potatoes Carrots Radishes
## 1 1 19.525 20.230 21.245
## 2 2 20.435 21.160 19.405
## 3 3 20.540 21.215 19.245
## 4 4 20.955 19.035 21.010
## 5 5 20.605 20.225 20.170
## 6 6 21.530 20.320 19.150
Use qplot() to create a histogram of the means for each reshuffled group. Or, if you want a challenge, use ggplot() to overlay all 3 histograms in the same figure. How do the distributions of reshuffled means compare to the original means?
## Warning in plot.window(xlim, ylim, "", ...): "length.out" is not a graphical
## parameter
## Warning in title(main = main, sub = sub, xlab = xlab, ylab = ylab, ...):
## "length.out" is not a graphical parameter
## Warning in axis(1, ...): "length.out" is not a graphical parameter
## Warning in axis(2, at = yt, ...): "length.out" is not a graphical parameter
## $breaks
## [1] 19.0 19.5 20.0 20.5 21.0 21.5 22.0 22.5
##
## $counts
## [1] 7 20 34 25 12 1 1
##
## $density
## [1] 0.14 0.40 0.68 0.50 0.24 0.02 0.02
##
## $mids
## [1] 19.25 19.75 20.25 20.75 21.25 21.75 22.25
##
## $xname
## [1] "FrameWork$Carrots"
##
## $equidist
## [1] TRUE
##
## attr(,"class")
## [1] "histogram"
## Warning in plot.window(xlim, ylim, "", ...): "length.out" is not a graphical
## parameter
## Warning in title(main = main, sub = sub, xlab = xlab, ylab = ylab, ...):
## "length.out" is not a graphical parameter
## Warning in axis(1, ...): "length.out" is not a graphical parameter
## Warning in axis(2, at = yt, ...): "length.out" is not a graphical parameter
## $breaks
## [1] 19.0 19.5 20.0 20.5 21.0 21.5 22.0
##
## $counts
## [1] 11 24 25 29 8 3
##
## $density
## [1] 0.22 0.48 0.50 0.58 0.16 0.06
##
## $mids
## [1] 19.25 19.75 20.25 20.75 21.25 21.75
##
## $xname
## [1] "FrameWork$Potatoes"
##
## $equidist
## [1] TRUE
##
## attr(,"class")
## [1] "histogram"
## Warning in plot.window(xlim, ylim, "", ...): "length.out" is not a graphical
## parameter
## Warning in title(main = main, sub = sub, xlab = xlab, ylab = ylab, ...):
## "length.out" is not a graphical parameter
## Warning in axis(1, ...): "length.out" is not a graphical parameter
## Warning in axis(2, at = yt, ...): "length.out" is not a graphical parameter
## $breaks
## [1] 19.0 19.5 20.0 20.5 21.0 21.5 22.0
##
## $counts
## [1] 15 18 21 26 17 3
##
## $density
## [1] 0.30 0.36 0.42 0.52 0.34 0.06
##
## $mids
## [1] 19.25 19.75 20.25 20.75 21.25 21.75
##
## $xname
## [1] "FrameWork$Radishes"
##
## $equidist
## [1] TRUE
##
## attr(,"class")
## [1] "histogram"