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)}
To add columns with square of each column in R data frame, we can use setNames function and cbind function for squaring each value. This might be required when we want to use squared form of variables in the data analysis.
If r is a number, square(r) is a shortcut for creating a window object representing the square [ 0 , r ] × [ 0 , r ] . It is equivalent to the command owin(c(0,r),c(0,r)) . If r is a vector of length 2, then square(r) creates the square with x and y coordinates ranging from r[1] to r[2] .
Calculate Square root of a number in R Language – sqrt() Function. sqrt() function in R Language is used to calculate the mathematical square-root of the value passed to it as argument. Here, in the above code, NaN is produced because we passed a negative value in the sqrt() function.
R Programming is. The is. vector() function allows you to check if the object provided as an argument to it is a vector or not. This function takes an argument as an input and returns TRUE if the provided object is a vector. If the provided object is not a vector, this function returns FALSE.
Try this (faster and simpler):
newData <- data^2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With