Home > other >  Group values based on delta in R
Group values based on delta in R

Time:12-09

I have the following dataframe called df (dput below):

   value
1    2.0
2    2.0
3    5.0
4    1.0
5    2.1
6    3.0
7    3.1
8    2.9
9    4.0
10   4.2

I would like to group values including a specific delta in this case 0.1. This means that values 2.0, 2.0 and 2.1 form a group because they all fall within the delta. The values 4 and 4.2 for example don't form a group because it is not within the delta. But the values 3.0, 3.1 and 2.9 are a group because they are within 3 - delta. Here is the desired output:

   value group
1    2.0     1
2    2.0     1
3    5.0     2
4    1.0     3
5    2.1     1
6    3.0     4
7    3.1     4
8    2.9     4
9    4.0     5
10   4.2     6

So I was wondering if anyone knows how to group values with a specific delta in R?


dput of df:

df <- structure(list(value = c(2, 2, 5, 1, 2.1, 3, 3.1, 2.9, 4, 4.2
)), class = "data.frame", row.names = c(NA, -10L))

CodePudding user response:

f <- function(x, d){
  cs <- cumsum(diff(c(0, sort(x))) > (d   .Machine$double.eps))
  match(cs[rank(x)], unique(cs[rank(x)]))
}

f(df$value, 0.1)
#[1] 1 1 2 3 1 4 4 4 5 6

f(c(0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1), 0.1)
#[1] 1 1 1 1 1 1 1 1 1 1
  • Related