<- c("economics", "econometrics", "ECON 4750")
x
library(stringr)
str_length(x)
[1] 9 12 9
<- c("economics", "econometrics", "ECON 4750")
x
library(stringr)
str_length(x)
[1] 9 12 9
Part a
<- function(n) {
fibonacci
# handle cases where n=1 or 2
if (n == 1) {
return(0)
}
if (n==2) {
return(1)
}
# main code
<- c(0,1)
fib_seq
for (i in 3:n) {
<- fib_seq[i-1] + fib_seq[i-2]
fib_seq[i]
}
fib_seq[n] }
fibonacci(5)
[1] 3
fibonacci(8)
[1] 13
Part b
<- function(a,b,n) {
alt_seq # handle cases where n=1 or 2
if (n == 1) {
return(a)
}
if (n==2) {
return(b)
}
# main code
<- c(a,b)
this_seq
for (i in 3:n) {
<- this_seq[i-1] + this_seq[i-2]
this_seq[i]
}
this_seq[n] }
alt_seq(3,7,4)
[1] 17
Part a
nrow(iris)
[1] 150
Part b
mean(iris$Sepal.Length)
[1] 5.843333
Part c
mean(subset(iris, Species=="setosa")$Sepal.Width)
[1] 3.428
Part d
<- iris[order(iris$Petal.Length),]
sorted_iris 1:10,] sorted_iris[
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
23 4.6 3.6 1.0 0.2 setosa
14 4.3 3.0 1.1 0.1 setosa
15 5.8 4.0 1.2 0.2 setosa
36 5.0 3.2 1.2 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
17 5.4 3.9 1.3 0.4 setosa
37 5.5 3.5 1.3 0.2 setosa
39 4.4 3.0 1.3 0.2 setosa
41 5.0 3.5 1.3 0.3 setosa
42 4.5 2.3 1.3 0.3 setosa