3

私はRにまったく慣れておらず、変数の1つの範囲値を使用して2つのデータフレーム間でサブセットと再結合について質問があります。だから私はこのような私の2つのデータフレームを持っています:

        x         y                         
 [1,] 79.00     19.63
 [2,] 79.01     19.58
 [3,] 79.02     19.57
 [4,] 79.03     19.58
 [5,] 79.04     19.60
 [6,] 79.05     19.65
 [7,] 79.06     19.67
 [8,] 79.07     19.70
 [9,] 79.08     19.67
[10,] 79.09     19.72

          id        min_x  max_x
[1,] 7G005-1010-10  79.01  79.06  
[2,] 7G100-0001-10  79.02  79.09
[3,] 8S010-1201-10  79.06  79.09

私の目的は、次のように2つを組み合わせることにあります。

     id           x       y
7G005-1010-10   79,01   19,58
7G005-1010-10   79,02   19,57
7G005-1010-10   79,03   19,58
7G005-1010-10   79,04   19,6
7G005-1010-10   79,05   19,65
7G005-1010-10   79,06   19,7
7G100-0001-10   79,02   19,57
     ...         ...     ...

私のデータフレームの出力でわかるように、私はdata.tableパッケージを使用して問題を解決する方法を見つけようとしています。

さて、誰かがそれをどのように扱うか(の有無にかかわらずdata.table)教えてくれるなら!

前もって感謝します。

英語が下手でごめんなさい。

4

1 に答える 1

4

これはdata.tableうまくできません。実装するのはFR#203です。xtsこの操作があると思うので、パッケージを試すことができます。

長くて不格好な方法(テストされていない)の1つdata.tableは、次のとおりです。最初のテーブルがPで、範囲を含む2番目のテーブルがであるとしRます。

setkey(P,x)
# sort by x and mark as sorted so future queries can use binary search on P

from = P[J(R$min_x),which=TRUE]
# Lookup each min_x in the key of P, returning the location. J stands for Join.

to = P[J(R$max_x),which=TRUE]
# Lookup each max_x in the key of P, returning the location.

len = to-from+1
# vectorized for each item the length to[i]-from[i]+1

i = unlist(mapply("seq.int",from,to,SIMPLIFY=FALSE))
# for each item the sequence from[i]:to[i], then concat them all into one vector

cbind(rep(R$id,len), P[i])
# use len to expand the items of R to match what they match to in P
于 2012-06-22T17:07:24.263 に答える