3

次のSQLクエリと同等のdata.tableを取得する方法はありますか?

create C as select * from R,P where 
P.x between R.min_x and R.max_x and P.var2 < R.col3

私の問題は、R,PRがクラッシュするので、デカルト積ができないことです。どのテクニックにも満足しています(いくつかのステップにある場合でも...)

一般的なサイズは、R 1K行、P3M行です。

編集:

library(data.table)
R = data.table(min_x=c(.6,.4,.01,.8),max_x=c(.7,.51,.05,.95),col3=c(.6,.4,1.2,.6))
P = data.table(x=seq(.1,.9,.1),var2=c(1,.4,.3,.2,0,.5,.65,.7,0))
setkey(P, x)
setkey(R,min_x,max_x) #and max_x is always > min_x

#R
#   min_x max_x col3
#1:  0.01  0.05  1.2
#2:  0.40  0.51  0.4
#3:  0.60  0.70  0.6
#4:  0.80  0.95  0.6

#B
#      x var2
#1:  0.1 1.00 => var1 not in any [col1,col2]
#2:  0.2 0.40 => same
#3:  0.3 0.30 => same
#4:  0.4 0.20 => .4 in [.4,.51] but .2 < .4 so NO
#5:  0.5    0 => same 
#6:  0.6 0.50 => .6 in [.6, .7] but .5 < .6 so NO
#7:  0.7 0.65 => .6 in [.6, .7] AND .65 > .6 => SELECTED
#8:  0.8 0.70 => YES
#9:  0.9    0 => NO

だから期待される結果

#  min_x max_x  col3    x var2
#1: 0.60  0.70  0.6  0.70 0.65 
#2: 0.80  0.95  0.6  0.80 0.70
4

2 に答える 2

1

このFRが実装されている場合(およびそのリンクが役立つ場合があります):

FR#203 2列で%between%ではなくiの範囲を指定できるようにする

それは可能性があります :

setkey(B, var1, var2)
B[A[,list(.(col1,col2),.(-Inf,col3))], j]

それでよろしいですか?潜在的なデカルト拡張をメモリに保存するために、jグループごと(の行ごと)に実行されるを指定する必要があります。iしかし、本当に大きなテーブルを返したい場合は、allow.cartesianフラグを設定できます。

B[A[,list(.(col1,col2),.(-Inf,col3))], allow.cartesian=TRUE]

もちろん、これは今のところ実行できないので、これは単なる探索的な答えです。

于 2013-03-26T18:34:02.900 に答える
0

私はこの投稿から@MatthewDowleの助けを借りて答えを得ることになりました

setkey(P,x)
# sort by x and mark as sorted so future queries can use binary search on P
# Lookup each min_x in the key of P, returning the location. J stands for Join.
from = P[J(R$min_x), roll=-Inf, mult='first', which=TRUE]
# Lookup each max_x in the key of P, returning the location.
to = P[J(R$max_x),roll=Inf, mult='last', which=TRUE]
# vectorized for each item the length to[i]-from[i]+1
len = to-from+1
#get NA that can occur if no x > min_x  
isNaFromTo = !is.na(from) & !is.na(to)
#remove the NA from from/to
to = to[isNaFromTo]
from = from[isNaFromTo]
#replace NA by 0 in len which will flag the fact that we want to remove the line from R
len[!isNaFromTo] = 0;
# create index for P
i = unlist(mapply("seq.int",from,to,SIMPLIFY=FALSE))
# create index of R 
j = rep(1:nrow(R), len);
#bind to get the result
res = cbind(R[j], P[i]) 
res = res[var2>col3]

期待どおりの結果

   min_x max_x col3   x var2
1:   0.6  0.70  0.6 0.7 0.65
2:   0.8  0.95  0.6 0.8 0.70
于 2013-03-27T12:27:18.510 に答える