Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

Count the number of values between value and value - x by variable

$
0
0

I have some data:

library(data.table)
set.seed(1)
df1 <- data.frame(let=sample(sample(letters,2),5, replace=TRUE),
                  num=sample(1:10,5))
setDT(df1)
   let num
1:   j   7
2:   j   6
3:   g   1
4:   j   2
5:   j  10

and I would like to calculate the number of num that are less than or equal to num AND are greater than or equal to num - 4, by let. Using the data.table package would be preferable, but any solution using dplyr or base r would be fine too. The output would look like this:

   let num countNumByLet
1:   j   7             2
2:   j   6             2
3:   g   1             1
4:   j   2             1
5:   j  10             3

Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>