as.numeric() 在 R 中有什么用?

What is the use of as.numeric() in R?

这是 金融工程统计和数据分析 第 252 页的示例 1,作者:Ruppert:

This problem and the next use CRSP daily returns. First, get the data and plot the ACF in two ways:

library(Ecdat)
data(CRSPday)
crsp=CRSPday[,7]
acf(crsp)
acf(as.numeric(crsp))

Explain what “lag” means in the two ACF plots. Why does lag differ between the plots?

我有 运行 代码并得到了两个 acf 图:

似乎只有这些图的x轴标签不同。这是为什么?在这种情况下使用 as.numeric 是什么?非常感谢!

@Jeremy Miles 是正确的。

数据种类繁多。简单的是整数、二进制 (T/F) 变量、字符数组和双精度数。 R 拥有其他品种和数据形式的整个动物园(双关语)。其中之一是 "time series".

您可以使用class函数验证"crsp"是一个时间序列对象:

class(crsp)

如果要确定哪些方法适用于此 class,请使用 "methods" 函数:

> methods(class=class(crsp))

 [1] [.ts*            [<-.ts*          aggregate.ts     as.data.frame.ts
 [5] cbind.ts*        cycle.ts*        diff.ts*         diffinv.ts*     
 [9] kernapply.ts*    lines.ts*        monthplot.ts*    na.omit.ts*     
[13] Ops.ts*          plot.ts          print.ts*        t.ts*           
[17] time.ts*         window.ts*       window<-.ts*    

现在,如果您想将它与 "numeric" 对象进行比较,则只需使用 "as.numeric"。

> class(as.numeric(crsp))

[1] "numeric"

您还可以看到可用方法的区别:

时间序列的一个特点是时间值隐藏在其中。当您将其转换为数字时,您将这些换成行索引。