Example function
add_3 <- function(arg1) {
new_value <- arg1 + 3
return(new_value)
}
add_3(10)
## [1] 13
Example for loop (with print outs to help you navigate what’s going on! - thank you Lucy and Michael :D )
x <- c(13, 14, 22, 100, 7, 4)
print(x)
## [1] 13 14 22 100 7 4
for (i in 1:length(x)) {
print(i)
numn <- x[i]
print(numn)
numprint <- paste("n =", numn)
print(numprint)
}
## [1] 1
## [1] 13
## [1] "n = 13"
## [1] 2
## [1] 14
## [1] "n = 14"
## [1] 3
## [1] 22
## [1] "n = 22"
## [1] 4
## [1] 100
## [1] "n = 100"
## [1] 5
## [1] 7
## [1] "n = 7"
## [1] 6
## [1] 4
## [1] "n = 4"