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

时间:2020-02-23 14:43:55  来源:igfitidea点击:

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

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

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

R中sqrt()的基本用法

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

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

输出-> 15

在Vector上的R中使用sqrt()

其中我们将计算向量中存在的值的平方根。
向量只是多个值的集合。
执行以下代码以获得向量的平方根。

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

输出-> 12 15 8 9 13 10 20

在空气质量数据集中找到温度值的平方根

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

执行以下代码以查找温度的平方根。

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

输出->

查找复数的平方根

其中我们将使用R Studio中的sqrt()计算复数的平方根。
执行以下代码以计算平方根。

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

输出= 1.098684 + 0.45509i

在矩阵上的R中使用sqrt()

其中我们将创建一个矩阵,然后找到矩阵中存在的值的平方根。
我们正在找到矩阵整个值以及矩阵每一列的平方根。

执行以下代码进行计算。

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

输出

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

现在,我们对矩阵y进行平方根运算。

sqrt(y)

输出=

[,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

让我们在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()查找平方根时的常见错误

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

  • 错误1 =产生NaN
  • 错误2 =非数字参数

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

1.查找负值的平方根时出错

通常,如果在R中找到负数的平方根,则会遇到错误。

错误是–>

Warning message:
In sqrt(x) : NaNs produced

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

下面的代码将演示该操作。

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

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

错误是->

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

为了克服这个问题,我们使用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