3

I am using the data of the rugarch package:

  library(rugarch)
  data(sp500ret)

The data look like:

head(sp500ret)
               SP500RET
1987-03-10  0.008840447
1987-03-11 -0.001892734
1987-03-12  0.003129678
1987-03-13 -0.004577455
1987-03-16 -0.005742768
1987-03-17  0.014603325

I just want to have the first e.g. 1000 values, so I tried

sp500retmod<-sp500ret[-c(1001:length(sp500ret[,1])),1]

But this gives

head(sp500retmod)

[1]  0.008840447 -0.001892734  0.003129678 -0.004577455 -0.005742768
[6]  0.014603325

So the rownames are deleted, how can I get the first 1000 values and keep the rowname, the date?

I also tried

sp500retmod<-sp500ret[-c(1001:length(sp500ret[,1])),] 

but this does also not work.

4

2 回答 2

7

如果要使用该"["功能,可以使用:

sp500ret[seq(1000), , FALSE]

FALSE是参数的参数drop。默认值为TRUE,但这会将单列数据框转换为向量。因此行名将丢失。如果您指定FALSE,您将收到一个带有原始行名的数据框。

于 2013-06-12T15:40:25.273 回答
1

我认为这两个工作:

d1 <- head(sp500ret, n=1000L)
rownames(d1)

d2 <- subset(sp500ret, c(1:length(SP500RET)) < 1001)
rownames(d2)

# Solution from @Penguin_Knight
d3 <- subset(sp500ret, row(sp500ret) < 1001)
rownames(d3)
于 2013-06-12T14:50:42.530 回答