How to Use sqrt() in R to Find the Square Root?

Filed Under: R Programming
Square Root In R Using The Function Sqrt()

Getting a square root of the values in R is easy with the function sqrt() in R. Let’s find how.

sqrt() is a mathematical function in R, which is helpful to find the square root of the values of a vector. In this tutorial, we will see how to find the square root of the number or a vector in various aspects.

The syntax of the R square root function is – sqrt()


Basic Usage of sqrt() in R

In this section, we will calculate the square root of a number using the function, sqrt() in R studio.

#assigns a number to 'x'
x<-225
#claculates the square root of the number
sqrt(x)

Output -> 15


Using sqrt() in R on Vectors

Here we will calculate the square root of the values present in a vector. A vector is simply a collection of multiple values. Execute the below code to get the square root of the vector.

#creates a vector having multiple values
df<- c(144,225,64,81,169,100,400)
#calculates the square root of the vector
sqrt(df)

Output -> 12 15 8 9 13 10 20


Finding the square root of the Temperature values in airquality dataset

Here we can import the airquality dataset, which is an in-built dataset in R, and find the square root of the Temperature column present there.

Execute the below code to find the sqrt of Temperature.

#reads the values
datasets::airquality
#calculates the square root of the temperature values
sqrt(airquality$Temp)

Output ->

Square Root In R

Finding the square root of the Complex number

Here we are going to calculate the square root of the complex number using sqrt() in R studio. Execute the below code to calculate the square root.

#assigns a complex number
x<-1+1i
#calculates the square root of the complex number
sqrt(x)

Output = 1.098684+0.45509i


Using sqrt() in R on a Matrix

Here we are going to create a matrix and let’s find the square root of the values present in the matrix. We are finding the square root for the entire values of the matrix and also for each column of the matrix as well.

Execute the below code for the calculation.

> y<-matrix(c(4,8,12,16,20,24,28,32,36,40,44,48), nrow=3, ncol=4)
> y

Output –

[,1] [,2] [,3] [,4]
[1,] 4 16 28 40
[2,] 8 20 32 44
[3,] 12 24 36 48

Let’s now perform the square root operation on the matrix y.

sqrt(y)

Output=

[,1] [,2] [,3] [,4]
[1,] 2.000000 4.000000 5.291503 6.324555
[2,] 2.828427 4.472136 5.656854 6.633250
[3,] 3.464102 4.898979 6.000000 6.928203

Let’s play around with sqrt() in R method to find square roots of different columns and rows.

#calculates the square root for all the values in columns
sqrt(y[,1])
 2.000000 2.828427 3.464102
sqrt(y[,2])
 4.000000 4.472136 4.898979
sqrt(y[,3])
 5.291503 5.656854 6.000000
sqrt(y[,4])
 6.324555 6.633250 6.928203

#calcultes the square root for all the values in rows
sqrt(y[1,])
 2.000000 4.000000 5.291503 6.324555
sqrt(y[2,])
 2.828427 4.472136 5.656854 6.633250
sqrt(y[3,])
 3.464102 4.898979 6.000000 6.928203

Common Errors When Using sqrt() in R to find the square root

Finding the square root of the values in R is easy. But sometimes you will encounter some errors while doing it. Below I have mentioned some possible errors and how to deal with them.

  • Error 1 = NaNs produced
  • Error 2 = non numeric argument

If you find any errors in your process, kindly handle them as shown in the below methods.


1. Error in finding the Square root of the negative values

Generally, you will encounter an error if you find the square root of the negative number in R.

The error is – >

Warning message:
In sqrt(x) : NaNs produced

To overcome this kind of error, let’s use the abs() function. This function will convert the negative value to absolute value and thereby facilitates the process of calculating the square root.

The below code will demonstrate the action.

#creates a vector including the negetive values
v<- c(24,65,-87,45,-67)
#calculates the square root
sqrt(v)
#the function excludes the negative values and shows error

Output = 4.898979 8.062258      NaN 6.708204      NaN

Warning message:
In sqrt(v) : NaNs produced
#adds the abs() function to convert the negetive values into absolute numbers i.e. positive numbers 
v<- c(24,65,-87,45,-67)
sqrt(abs(v))

Output = 4.898979 8.062258 9.327379 6.708204 8.185353

2. Error in finding the Square root of the non-numeric values

R will not accept strings of numbers to apply the square root function. If you mention the number as a string, R will show the error as shown below.

The error is ->

Error in sqrt(df) : 
non-numeric argument to mathematical function

To overcome this, we are using the function as.numeric(), which converts the characters into numeric values. This is illustrated below.

#creates a vector having charecters
df<-c('25','81','64')
#calculates the square root and encounters an error
sqrt(df)

Error in sqrt(df) : non-numeric argument to mathematical function

df<-c('25','81','64')
#adds the as.numeric() function to convert the charecter as numbers. 
sqrt(as.numeric(df))

Output = 5 9 8

Conclusion

R always provides good functions to compute mathematical operations and the function sqrt() is no different from that. It calculates the square root of the values resent in vectors, txt files, and CSV files.

You may experience an error when your values include negative and NA values. This tutorial has addressed those issues and listed methods to overcome that. Implement those if you found any errors in finding the square root of values.

For any queries, kindly mention it in the comments section. Happy learning!!!

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages