Use the code below to re-define the standardize()
function and to create a vector with standardized numbers.
standardize <- function (x) {
(x - mean(x, na.rm = TRUE)) / sd(x, na.rm = TRUE)
}
standardized_vector <-
standardize(sample(1:100, 30))
for()
-loop, loop through each element of the vector, add the number pi
, and print the result.
pi
. Just type pi
and you’ll receive 3.141593. You need to wrap what the function does within the print()
function.
for (number in standardized_vector) {
print(number + pi)
}
## [1] 2.03279
## [1] 1.781107
## [1] 2.997574
## [1] 4.339883
## [1] 2.871733
## [1] 3.962358
## [1] 2.242526
## [1] 2.955627
## [1] 1.906948
## [1] 3.878464
## [1] 5.598297
## [1] 4.130147
## [1] 1.697213
## [1] 3.081468
## [1] 3.920411
## [1] 2.284473
## [1] 2.703944
## [1] 3.626781
## [1] 2.116684
## [1] 2.32642
## [1] 4.004306
## [1] 2.494208
## [1] 3.039521
## [1] 4.591566
## [1] 2.200578
## [1] 3.333151
## [1] 2.158631
## [1] 4.759354
## [1] 3.542887
## [1] 3.668728
sapply()
function here.
print()
function anymore.
sapply(standardized_vector, function (number) {
number + pi
})
## [1] 2.032790 1.781107 2.997574 4.339883 2.871733 3.962358 2.242526 2.955627 1.906948 3.878464 5.598297 4.130147 1.697213 3.081468
## [15] 3.920411 2.284473 2.703944 3.626781 2.116684 2.326420 4.004306 2.494208 3.039521 4.591566 2.200578 3.333151 2.158631 4.759354
## [29] 3.542887 3.668728
lapply()
or map()
from the purrr
package.
library(purrr)
lapply(standardized_vector, function (number) {
number + pi
})
## [[1]]
## [1] 2.03279
##
## [[2]]
## [1] 1.781107
##
## [[3]]
## [1] 2.997574
##
## [[4]]
## [1] 4.339883
##
## [[5]]
## [1] 2.871733
##
## [[6]]
## [1] 3.962358
##
## [[7]]
## [1] 2.242526
##
## [[8]]
## [1] 2.955627
##
## [[9]]
## [1] 1.906948
##
## [[10]]
## [1] 3.878464
##
## [[11]]
## [1] 5.598297
##
## [[12]]
## [1] 4.130147
##
## [[13]]
## [1] 1.697213
##
## [[14]]
## [1] 3.081468
##
## [[15]]
## [1] 3.920411
##
## [[16]]
## [1] 2.284473
##
## [[17]]
## [1] 2.703944
##
## [[18]]
## [1] 3.626781
##
## [[19]]
## [1] 2.116684
##
## [[20]]
## [1] 2.32642
##
## [[21]]
## [1] 4.004306
##
## [[22]]
## [1] 2.494208
##
## [[23]]
## [1] 3.039521
##
## [[24]]
## [1] 4.591566
##
## [[25]]
## [1] 2.200578
##
## [[26]]
## [1] 3.333151
##
## [[27]]
## [1] 2.158631
##
## [[28]]
## [1] 4.759354
##
## [[29]]
## [1] 3.542887
##
## [[30]]
## [1] 3.668728
standardized_vector %>%
map(~.x + pi)
## [[1]]
## [1] 2.03279
##
## [[2]]
## [1] 1.781107
##
## [[3]]
## [1] 2.997574
##
## [[4]]
## [1] 4.339883
##
## [[5]]
## [1] 2.871733
##
## [[6]]
## [1] 3.962358
##
## [[7]]
## [1] 2.242526
##
## [[8]]
## [1] 2.955627
##
## [[9]]
## [1] 1.906948
##
## [[10]]
## [1] 3.878464
##
## [[11]]
## [1] 5.598297
##
## [[12]]
## [1] 4.130147
##
## [[13]]
## [1] 1.697213
##
## [[14]]
## [1] 3.081468
##
## [[15]]
## [1] 3.920411
##
## [[16]]
## [1] 2.284473
##
## [[17]]
## [1] 2.703944
##
## [[18]]
## [1] 3.626781
##
## [[19]]
## [1] 2.116684
##
## [[20]]
## [1] 2.32642
##
## [[21]]
## [1] 4.004306
##
## [[22]]
## [1] 2.494208
##
## [[23]]
## [1] 3.039521
##
## [[24]]
## [1] 4.591566
##
## [[25]]
## [1] 2.200578
##
## [[26]]
## [1] 3.333151
##
## [[27]]
## [1] 2.158631
##
## [[28]]
## [1] 4.759354
##
## [[29]]
## [1] 3.542887
##
## [[30]]
## [1] 3.668728