2013-03-26 75 views
0

我有一個輸出生成的裁減功能,這是下面的...讓我們調用這個輸出'數據'。從輸出R程序訪問數據

cuts: [20,25) 
    Time Kilometres 
21 20  7.3 
22 21  8.4 
23 22  9.5 
24 23  10.6 
25 24  11.7 
------------------------------------------------------------ 
cuts: [25,30) 
    Time Kilometres 
26 25  12.8 
27 26  13.9 
28 27  15.0 
29 28  16.1 
30 29  17.2 
------------------------------------------------------------ 
cuts: [30,35) 
    Time Kilometres 
31 30  18.3 
32 31  19.4 
33 32  20.5 
34 33  21.6 
35 34  22.7 

我怎麼能訪問每個數據cut..like割傷得到公里數據:[20,25] ..等我試着做數據$公里......但是,這並不工作.. 。所以我基本上要一個新的數據幀在那裏我可以seperately使用公里的數據爲每削減

+1

我假設您使用'by'來生成此項。你使用了什麼命令? – 2013-03-26 07:48:09

+0

@ sebastian-c,'by'確實有明顯的視覺輸出,不是。看原始問題[這裏](http://stackoverflow.com/questions/15628289/cut-function-in-r-program) – A5C1D2H2I1M1N2O1R2T1 2013-03-26 07:52:43

回答

0

by這裏的輸出是一個列表,這樣你就可以使用基本目錄索引,無論是數字或名稱。從your question from a few hours ago中的數據,並the answer from Matthew Lundberg,我們能指標如下:

> x[[1]] 
    Time Velocity 
1 0.0  0.00 
2 1.5  1.21 
3 3.0  1.26 
4 4.5  1.31 
> x[["[6,12)"]] 
    Time Velocity 
5 6.0  1.36 
6 7.5  1.41 
7 9.0  1.46 
8 10.5  1.51 

您可以通過使用str審查對象的結構在R上。這對於幫助您決定如何提取某些信息通常很有用。這裏的str(x)

> str(x) 
List of 7 
$ [0,6) :Classes ‘AsIs’ and 'data.frame': 4 obs. of 2 variables: 
    ..$ Time : num [1:4] 0 1.5 3 4.5 
    ..$ Velocity: num [1:4] 0 1.21 1.26 1.31 
$ [6,12) :Classes ‘AsIs’ and 'data.frame': 4 obs. of 2 variables: 
    ..$ Time : num [1:4] 6 7.5 9 10.5 
    ..$ Velocity: num [1:4] 1.36 1.41 1.46 1.51 
$ [12,18):Classes ‘AsIs’ and 'data.frame': 6 obs. of 2 variables: 
    ..$ Time : num [1:6] 12 13 14 15 16 17 
    ..$ Velocity: num [1:6] 1.56 1.61 1.66 1.71 1.76 1.81 
$ [18,24):Classes ‘AsIs’ and 'data.frame': 5 obs. of 2 variables: 
    ..$ Time : num [1:5] 18 19 20 21 22.5 
    ..$ Velocity: num [1:5] 1.86 1.91 1.96 2.01 2.06 
$ [24,30):Classes ‘AsIs’ and 'data.frame': 4 obs. of 2 variables: 
    ..$ Time : num [1:4] 24 25.5 27 28.5 
    ..$ Velocity: num [1:4] 2.11 2.16 2.21 2.26 
$ [30,36):Classes ‘AsIs’ and 'data.frame': 4 obs. of 2 variables: 
    ..$ Time : num [1:4] 30 31.5 33 34.5 
    ..$ Velocity: num [1:4] 2.31 2.36 2.41 2.42 
$ [36,42):Classes ‘AsIs’ and 'data.frame': 1 obs. of 2 variables: 
    ..$ Time : num 36 
    ..$ Velocity: num 2.43 
- attr(*, "dim")= int 7 
- attr(*, "dimnames")=List of 1 
    ..$ cuts: chr [1:7] "[0,6)" "[6,12)" "[12,18)" "[18,24)" ... 
- attr(*, "call")= language by.data.frame(data = mydf, INDICES = cuts, FUN = I) 
- attr(*, "class")= chr "by" 

從這裏我們可以看到,我們有七個項目命名列表,每個列表包含data.frame。因此,如果我們想要第三個區間的「速度」變量(第二列)的向量,我們將使用類似於:

> x[[3]][[2]] 
[1] 1.56 1.61 1.66 1.71 1.76 1.81