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!
example
contains only one column, so why are you selecting an unexsisting column usingexample[, 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 Arenburgpos
has only 6 values so its just being recycled. You are not comparing against all the values inpos
rather each value inexample[, 4]
compared against only one (recycled) value inpos
. Are you trying to compare each values inexample[, 4]
against each value inpos
? – David Arenburgindx <- sapply(example[, -4], function(x) any(x < pos + 25) | any(x > pos - 25))
. Then just doexample[indx, -4]
– David Arenburgexample[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 there – David Arenburg