R的奇怪行为 [英] Strange behavior of R

查看:102
本文介绍了R的奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我在R中有以下代码:

So, I have the following code in R:

y
a <- -0.1    
test <- (1/((y+as.numeric(!y))*(a-1)))  
test  
test^a  
-0.9090909^a 

给我输出:

> y  
 [1] 0.00000000 0.06024096 0.00000000 0.01098901 0.00000000 0.00000000
 [7] 0.01829268 0.00000000 0.06976744 0.00000000 0.04380665 0.01351351
[13] 0.00000000 0.00000000 0.00000000 0.00000000 0.00310559 0.00000000
[19] 0.00000000 0.00000000 0.09957447 0.00000000 0.03738318 0.00000000
> a <- -0.1
> test <- (1/((y+as.numeric(!y))*(a-1)))
> test
 [1]   -0.9090909  -15.0909091   -0.9090909  -82.7272727   -0.9090909
 [6]   -0.9090909  -49.6969697   -0.9090909  -13.0303030   -0.9090909
[11]  -20.7523511  -67.2727273   -0.9090909   -0.9090909   -0.9090909
[16]   -0.9090909 -292.7272727   -0.9090909   -0.9090909   -0.9090909
[21]   -9.1297591   -0.9090909  -24.3181818   -0.9090909  

> test^a  
 [1] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
[17] NaN NaN NaN NaN NaN NaN NaN NaN  

> -0.9090909^a  
[1] -1.009577

我不明白为什么在对幂a进行测试时会给我NaN,很明显,我可以简单地从测试中复制一个值并将其提高为幂a,这是可行的.使用test [1] ^ a也不起作用.

I do not understand why It gives me NaN when taking test to the power a, when clearly, I can simply copy a single value from test and raise it to the power a, and that works. Using test[1]^a does not work either.

推荐答案

-0.9090909 ^ a-(0.9090909 ^ a)相同.注意括号.

但是,您的test包含负值,并且您不能取负数的根.尝试(-0.9090909) ^ a进行验证:

However, your test contains negative values, and you cannot take the root of a negative number. Try (-0.9090909) ^ a to verify this:

> (-0.9090909) ^ -0.1
[1] NaN

这篇关于R的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆