1

Is there a simple way to evaluate a range and check if an integer is within that range?

Other than this post Check to see if a value is within a range in R? I did not find other relevant ones.

Example

range <- cut(rep(1,5),4) # Create intervals
range.test <- range[2]
# Now I want to check whether integer 1L is within the range.test (Of course it is)
Code comes here.

I tried to use findInterval and also convert the range.test into vector, or use seq, inrangeor other functions but failed.

As all the analysis is based on data.table, and this part of analysis forms part of whole practice of which the output is preferred to be one data.table, so I put tag data.table to make sure the consistency.

EDIT

The whole picture in the context of data.table.

dt <- data.table(structure(list(Time = c("2016-01-04 09:05:06", "2016-01-04 09:20:00","2016-01-04 09:30:00", "2016-01-04 09:30:01", "2016-01-04 09:30:02","2016-01-04 09:30:05", "2016-01-04 09:30:06", "2016-01-04 09:31:35","2016-01-04 09:31:38", "2016-01-04 09:32:33"), Price = c(105,104.1, 104.1, 103.9, 104.1, 104, 104.1, 104.1, 104.1, 104), Volume = c(9500L,23500L, 18500L, 12500L, 16118L, 13000L, 2500L, 300L, 500L, 500L), Flag = c(1L, 0L, 1L, 0L, 1L, 0L, 1L, 1L, 1L, 0L), Ticker = c("0001","0001", "0001", "0001", "0001", "0001", "0001", "0001", "0001","0001")), .Names = c("Time", "Price", "Volume", "Flag", "Ticker"), class = c("data.table", "data.frame"), row.names = c(NA, -10L)))
                   Time Price Volume Flag Ticker
 1: 2016-01-04 09:05:06 105.0   9500    1   0001
 2: 2016-01-04 09:20:00 104.1  23500    0   0001
 3: 2016-01-04 09:30:00 104.1  18500    1   0001
 4: 2016-01-04 09:30:01 103.9  12500    0   0001
 5: 2016-01-04 09:30:02 104.1  16118    1   0001
 6: 2016-01-04 09:30:05 104.0  13000    0   0001
 7: 2016-01-04 09:30:06 104.1   2500    1   0001
 8: 2016-01-04 09:30:07 104.1   1500    1   0001
 9: 2016-01-04 09:30:08 104.3    500    1   0001
10: 2016-01-04 09:30:10 104.0   1000    0   0001
11: 2016-01-04 09:30:11 103.9   1000    0   0001
12: 2016-01-04 09:30:15 104.0   3500    1   0001
13: 2016-01-04 09:30:17 104.3   2000    1   0001
14: 2016-01-04 09:30:19 104.3   1500    1   0001
15: 2016-01-04 09:30:20 104.4    500    1   0001
16: 2016-01-04 09:30:21 104.4   1500    1   0001
17: 2016-01-04 09:30:22 104.4   1000    1   0001
18: 2016-01-04 09:30:24 104.4   1500    1   0001
19: 2016-01-04 09:30:25 104.0   2000    0   0001
20: 2016-01-04 09:30:27 104.1   3500    1   0001
21: 2016-01-04 09:30:35 104.0    500    0   0001
22: 2016-01-04 09:31:14 104.1   5000    1   0001
23: 2016-01-04 09:31:15 104.1    500    1   0001
24: 2016-01-04 09:31:18 104.1   2500    1   0001
25: 2016-01-04 09:31:25 104.1   3000    1   0001
26: 2016-01-04 09:31:29 104.0   2000    0   0001
27: 2016-01-04 09:31:30 104.1    500    1   0001
28: 2016-01-04 09:31:35 104.1    300    1   0001
29: 2016-01-04 09:31:38 104.1    500    1   0001
30: 2016-01-04 09:32:33 104.0    500    0   0001

# First get the distribution of the Volume
    distribution <- dt[Flag == 1, sum(Volume), by = cut(Price, 5)][, percentage := list(V1/sum(V1))]
# Get the max range bin
Max_range <- distribution[which.max(percentage), cut]
# Get the Closing price
Closing_price <- dt[.N, Price]
# Check whether the closing price is in the Max_range
Code comes here[?????]

So here comes the question: for specific Ticker, how to check whether the closing price is within the specific range? Just a True or False is needed. If the closing_price is within the Max_range, the corresponding Signal would be True, otherwise it would be False.

EDIT 2

Added the desired output

The desired output

   Ticker Signal
1:   0001   False

So I would like to create one function to check whether the Signal is True or False and then update in the data.table.

Thanks a lot!

Community
  • 1
  • 1
Bigchao
  • 1,746
  • 3
  • 15
  • 31

2 Answers2

1

So do I understand correctly that you want to find for each ticker (001,002 etc) if there is a value that falls outside a given range?

If that's the issue you could use group_by function from dplyr and a logical expression:

group_by(dt,Ticker) %>%
   summarise(Signal=any(with(.,Price>max_price & Price<min_price)))
0

The range.test object is a factor variable with levels(range.test):

levels(range.test)
[1] "(0.999,0.9995]" "(0.9995,1]"     "(1,1.0005]"     "(1.0005,1.001]"

When you passed it to findInterval as the second argument it was coerced to the numeric value of 2, so this was the result:

> findInterval(1,2)
[1] 0

Which was what was supposed to happen because 1 is less than 2. If you really want a sequence of numeric values ranging from 0.999 to 1.001 with 5 values you would use seq:

> seq( 0.999,  1.001, length=5)
[1] 0.9990 0.9995 1.0000 1.0005 1.0010

You could then test in which interval of that vector the number 1.000 would lie:

> findInterval( 1, seq( 0.999,  1.001, length=5) )
[1] 3
IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • Thanks a lot @42-, I've made update to the original post to make it clear. Could you please provide any instructions? Many thanks! – Bigchao Jan 29 '17 at 07:30