I would like to square every value in data
, and I am thinking about using a for loop like this:
data = rnorm(100, mean=0, sd=1)
Newdata = {L = NULL; for (i in data) {i = i*i} L = i return (L)}
I would like to square every value in data
, and I am thinking about using a for loop like this:
data = rnorm(100, mean=0, sd=1)
Newdata = {L = NULL; for (i in data) {i = i*i} L = i return (L)}
Try this (faster and simpler):
newData <- data^2
This will also work
newData <- data*data
data^2
! –
Filigreed How about sapply
(not really necessary for this simple case):
newData<- sapply(data, function(x) x^2)
© 2022 - 2024 — McMap. All rights reserved.