'R finding data in table for conditional vector
I ran into an issue where I would appreciate some help. I am looking for a generalized solution that allows me to find the following values of the next e.g. 2 rows in a dataset. Currently my code is:
a <- c(9,5,2,5,7)
b <-c(12,4,8,13,10)
c <- c(4,1,9,9,3)
d <-c(8,3,15,2,7)
a1 <- cbind(a,b,c,d)
#vectorquantiles being the condition that needs to be met to then find the values of the next e.g. 2 rows in the original dataset
vectorquantiles <- rowQuantiles(a1, probs = .8, na.rm = TRUE)
a1 <- as.data.frame(a1)
for(j in 1:ncol(a1)){
s <- set(a1, i=which(a1[[j]] <= vectorquantiles), j=j, value=NA)
}
the final dataset should look like this:
a | b | c | d |
---|---|---|---|
NA | NA | NA | NA |
NA | 4 | NA | NA |
2 | 8 | NA | NA |
5 | NA | NA | 2 |
NA | 10 | NA | 7 |
basically for every value in s
I want the next 2 rows from the column where the value was found in the original dataset a1
.
My dataset is much larger in width and length therefore a gernalized solution is necessary.
I hope I was able to make this somewhat clear. This is my first question asked here so and I did not find it easy to formulate my issue.
I appreciate any help.
*alternative ways of solving this are appreciated too
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|