R is an open-source programming language and software environment for statistical computing and graphics. The R language is widely used among statisticians and data miners for developing statistical software and data analysis. Polls, surveys of data miners, and studies of scholarly literature databases show that R's popularity has increased substantially in recent years. Looking at the recent advancement in using the R programming language, we discuss how you can easily find the square root of a number in the R programming language using some explicit methods. So, let's get started!
How to Take Square Root in R?
Below are some of the methods by which you can get the square root in the R programming language:
Method 1: Using sqrt() function
R programming language possesses a large collection of in-built methods including the sqrt() function. This function helps to find the square root of a number by taking the given number as the function parameter inside the parentheses. For a better understanding of the sqrt() method, check out the below example:
For example:
x <- 49 sqrt(x)
Output:
[1] 7
Method 2: Using sqrt() on vectors
A vector is simply a collection of values. The square root of a vector consists of the square roots of each element in the vector. The following code demonstrates how to perform this function:
For example:
x <- c(2, 5, 9, 16, 22, 36) sqrt(x)
Output:
[1] 1.414214 2.236068 3.000000 4.000000 4.690416 6.000000
One reason we can apply the sqrt function to a variable or column stored in a data.frame or matrix is that the function applies its calculation to each element of the matrix. Running the program is generally trouble-free, but some errors and warnings may occur.
Method 3: Using sqrt() on a matrix
Here, we'll make a matrix and then find the square root of the values in the matrix. We are calculating the square root of the matrix's total values as well as the square root of each column of the matrix. Check out the below example for a better understanding
For example:
y<-matrix(c(2, 4, 9, 12, 16, 21, 25, 36, 49), nrow=3, ncol=3) y
Output:
[,1] [,2] [,3] [1,] 2 12 25 [2,] 4 16 36 [3,] 9 21 49
Method 4: Square root of the column in dataFrame
You can also find the square root in R of the column in the dataframe using an R programming language. To do the same, make use of the dollar sign($) between the dataframe name and column name as shown in the below example:
For example:
data <- data.frame(a=c(2, 4, 6, 8, 9, 1), b=c(11, 14, 16, 20, 21, 24), c=c(10, 9, 5, 4, 2, 1), d=c(16, 25, 16, 27, 36, 81)) sqrt(data$a=b)
Output:
[1] 3.316625 3.741657 4.000000 4.472136 4.582576 4.898979
Method 5: Square the root of several columns in dataframe
You can find the square root of multiple columns in a dataframe using the R in-build method named apply(). The method takes the column name as the parameter whose square root needs to be found. Check out the below example for a better understanding of this method:
For example:
data <- data.frame(a=c(2, 4, 6, 8, 9, 1), b=c(11, 14, 16, 20, 21, 24), c=c(10, 9, 5, 4, 2, 1), d=c(16, 25, 16, 27, 36, 81)) apply(data[ , c('b', 'c')], 2, sqrt)
Output:
b c [1,] 3.316625 3.162278 [2,] 3.741657 3.000000 [3,] 4.000000 2.236068 [4,] 4.472136 2.000000 [5,] 4.582576 1.414214 [6,] 4.898979 1.000000
In R, finding the square root of a value is simple. However, you will occasionally make mistakes while doing it. I've listed some possible errors and how to deal with them below.
- NaNs produced
- Non-numeric Argument
Method 6: Warning message: In sqrt(x): NaNs produced
Whenever you try to calculate the square root of a negative value, the program generates a Warning message: In sqrt(): NaNs produced. To solve this problem, use the absolute function in combination with the square-root function; in other words, convert the negative value to its absolute value before applying root extraction. For a better understanding look at the below example:
For example:
x <- - 7 sqrt(x) x_sqrt <- sqrt(abs(x)) x_sqrt
Output:
[1] NaN Warning message: In sqrt(x) : NaNs produced [1] 2.645751
Method 7: Error in sqrt(x): non-numeric argument to mathematical function
Sometimes the sqrt function returns the error message as “Error in sqrt(x): non-numeric argument to mathematical function”. This error occurs when you try to calculate the square root of the character string.
For example:
x<-"7" sqrt(x)
Output:
Error in sqrt(x) : non-numeric argument to mathematical function Execution halted
To solve this issue, you can convert the string to an integer before computing the square root as shown in the below example:
For example:
x<-"7" sqrt(as.numeric(x))
Output:
[1] 2.645751
Conclusion
R always provides good functions for computing mathematical operations, and sqrt() is no exception. It computes the square root of the values contained in vectors, text files, and CSV files. In this article, we studied how you can find the square root of any number in R programming using various methods and its corresponding examples. If you're looking for r programming tutoring one-on-one, then our tutors are available 24/7.