データセットw
とキー変数x
の2つのケースがあります。バイナリサーチのような概念でサブセットデータを作成するR
Case 1:
x = 4
w = c(1,2,4,4,4,4,6,7,8,9,10,11,12,14,15)
Case2:
x = 12
w = c(1,2,4,4,4,4,6,7,8,9,10,11,12,14,15)
私は、データセットw
を通じてx
を検索するとw
でx
の場所ごとのような低級サイズのデータセットに元のデータセットのサブセットする関数を作成したいです。出力は検索キーと同じ上限値を持つより小さなサイズのデータセットになります。以下は、私がRに書き込みをしようとしている機能である:
create_chunk <- function(val, tab, L=1L, H=length(tab))
{
if(H >= L)
{
mid = L + ((H-L)/2)
## If the element is present within middle length
if(tab[mid] > val)
{
## subset the original data in reduced size and again do mid position value checking
## then subset the data
} else
{
mid = mid + (mid/2)
## Increase the mid position to go for right side checking
}
}
}
私は以下を探しています出力で:
Output for Case 1:
Dataset containing: 1,2,4,4,4,4
Output for Case 2:
Dataset containing: 1,2,4,4,4,4,6,7,8,9,10,11,12
Please note:
1. Dataset may contain duplicate values for search key and
all the duplicate values are expected in the output dataset.
2. I have huge size datasets (say around 2M rows) from
where I am trying to subset smaller dataset as per my requirement of search key.
新しいアップデート:ケース3
入力データ:
date value size stockName
1 2016-08-12 12:44:43 10093.40 4 HWA IS Equity
2 2016-08-12 12:44:38 10093.35 2 HWA IS Equity
3 2016-08-12 12:44:47 10088.00 2 HWA IS Equity
4 2016-08-12 12:44:52 10089.95 1 HWA IS Equity
5 2016-08-12 12:44:53 10089.95 1 HWA IS Equity
6 2016-08-12 12:44:54 10088.95 1 HWA IS Equity
検索キーは:10089.95
in colu mn。
予想される出力は次のようになります。ここでは
date value size stockName
1 2016-08-12 12:44:47 10088.00 2 HWA IS Equity
2 2016-08-12 12:44:54 10088.95 1 HWA IS Equity
3 2016-08-12 12:44:52 10089.95 1 HWA IS Equity
4 2016-08-12 12:44:53 10089.95 1 HWA IS Equity
あなたの機能にはどのような問題がありますか? – 989
2番目のデータセットでは成功しません。また、一致する変数が存在する場合は、重複した値を選択することを提案しました。 – Zico
'findInterval' - ' w [seq_len(findInterval(4、w))] ' –