2
votes

So I want to find values in a column of a data.frame, which are in range of defined values:

  example
 [1] 6 2 4 3 5 1
 pos
 [1] 1 3 2

I now want to get the values of example for which BOTH of the following statements are TRUE, so that I got only values that lie between pos - 1 and pos +1:

if(example < pos - 1)
if(example > pos + 1)

And now the real values for my task are within a data.frame. How do I extract the complete rows, which contain these values and build a new pos data.frame.

The expected output for the example would be:

result
[1] 2 3 1

Thanks in advance!

2
You said example contains only one column, so why are you selecting an unexsisting column using example[, 4]? Why wouldn't you provide a reproducible example for starters? I have a feeling that while trying to reproduce your error you will understand when it's coming from in the first place.David Arenburg
pos has only 6 values so its just being recycled. You are not comparing against all the values in pos rather each value in example[, 4] compared against only one (recycled) value in pos. Are you trying to compare each values in example[, 4] against each value in pos?David Arenburg
I think you are looking for something like indx <- sapply(example[, -4], function(x) any(x < pos + 25) | any(x > pos - 25)). Then just do example[indx, -4]David Arenburg
What is your expected output from the provided example?David Arenburg
So example[sapply(example, function(x) any(x > pos - 1) & any(x < pos + 1))] should work. 4 doesn't meet the second condition so shouldn't be thereDavid Arenburg

2 Answers

6
votes

Set min and max thresholds and then compare each element against them

maxind <- max(pos + 1)
minind <- min(pos - 1)

Then

example[sapply(example, function(x) x > minind & x < maxind)]
## [1] 2 3 1

Or, similarly

example[example > minind & example < maxind]
## [1] 2 3 1

Or using data.table

library(data.table)
example[between(example, minind, maxind, incbounds = FALSE)]
## [1] 2 3 1
3
votes

So the solution as suggested by @David Arenburg is like this:

indx <- sapply(example[, 4], function(x) any(x < pos + 1) & any(x > pos - 1)) 

Then just do

example[indx,]

This will search a data.frame in the specified column for values that are in the defined range and will just give you the rows of your desire!