不用sqrt实现平方根_如何在R中使用sqrt()查找平方根?

本文详细介绍了在R中使用sqrt()函数计算平方根的方法,包括基本用法、向量上的应用、在空气质量数据集上的操作,以及如何处理负值和非数值的平方根错误。此外,还展示了在矩阵上使用sqrt()的示例,帮助读者掌握在不同场景下计算平方根的技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

不用sqrt实现平方根

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

使用R中的sqrt()函数可以很容易地在R中获得值的平方根 。 让我们看看如何。

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.

sqrt()是R中的数学函数,有助于查找向量值的平方根。 在本教程中,我们将了解如何在各个方面找到数字或向量的平方根。

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

R平方根函数的语法为– sqrt()



R中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.

在本节中,我们将使用R studio中的函数sqrt()计算数字的平方根


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

Output -> 15

输出-> 15



在Vector上的R中使用sqrt() (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

输出-> 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.

在这里,我们可以导入空气质量数据集 ,这是R中的内置数据集,并找到其中存在的Temperature列的平方根。

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.

在这里,我们将使用R studio中的sqrt()计算复数的平方根。 执行以下代码以计算平方根。


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

Output = 1.098684+0.45509i

输出= 1.098684 + 0.45509i



在矩阵上的R中使用sqrt() (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.

现在让我们对矩阵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.

让我们在R方法中使用sqrt()来查找不同列和行的平方根。


#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


在R中使用sqrt()查找平方根时的常见错误 (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.

在R中找到值的平方根很容易。 但是有时您在执行操作时会遇到一些错误。 下面我提到了一些可能的错误以及如何处理它们。

  • Error 1 = NaNs produced

    错误1 =产生NaN
  • Error 2 = non numeric argument

    错误2 =非数字参数

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

如果您在处理过程中发现任何错误,请按照以下方法所示进行处理。



1.查找负值的平方根时出错 (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.

通常,如果在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.

为了克服这种错误,我们使用abs()函数 。 此函数将负值转换为绝对值,从而简化了平方根的计算过程。

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.查找非数值的平方根时出错 (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.

R 不会接受数字字符串来应用平方根函数。 如果将数字作为字符串提及,R将显示错误,如下所示。

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.

为了克服这个问题,我们使用函数as.numeric() ,它将字符转换为数字值。 如下所示。


#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.

R总是提供良好的函数来计算数学运算,并且该函数 sqrt()与此没有什么不同。 它计算向量,txt文件和CSV文件中发送的值的平方根。

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.

当您的值包含负值和NA值时,您可能会遇到错误。 本教程已解决了这些问题,并列出了克服这些问题的方法。 如果发现值的平方根时发现任何错误,请实施这些方法。

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

对于任何查询,请在评论部分中提及。 学习愉快!

翻译自: https://www.journaldev.com/39245/sqrt-in-r

不用sqrt实现平方根

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值