Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check to see if a value is within a range?

Tags:

r

I have a dataset in a data.table format that looks as such:

ID     time.s     time.e
1       1         2
2       1         4
3       2         3
4       2         4

I want to check to see if the value 1 is within time.s and time.e so that the end result would look like

[1] TRUE TRUE FALSE FALSE

How would I go about this? I have tried to use

 a[1 %in% seq(time.s, time.e)]

But all I get is all TRUE values. Any recommendations?

like image 283
akash87 Avatar asked Jan 16 '17 19:01

akash87


People also ask

How do you find if a value is in a range?

In Excel, to check if a value exists in a range or not, you can use the COUNTIF function, with the IF function. With COUNTIF you can check for the value and with IF, you can return a result value to show to the user. i.e., Yes or No, Found or Not Found.

How do you return a value if a given value exists in a certain range?

Return a value if a given value exists in a certain range by using a formula. Please apply the following formula to return a value if a given value exists in a certain range in Excel. 1. Select a blank cell, enter formula =VLOOKUP(E2,A2:C8,3, TRUE) into the Formula Bar and then press the Enter key.


2 Answers

Also, this works:

with(dat, time.s <= 1 & time.e >= 1)
like image 69
akash87 Avatar answered Oct 27 '22 07:10

akash87


Just adding to the tools that could be used to this, another being data.table::between:

data.frame <- data.frame(ID = 1:4,
                         time.s = c(1,1,2,2),
                         time.e = c(2,4,3,4))

data.table::between(1, data.frame$time.s, data.frame$time.e)
like image 43
Lalochezia Avatar answered Oct 27 '22 06:10

Lalochezia