简体   繁体   English

根据趋势分配组

[英]Assign groups based on the trend

I have searched a lot for this simple question, but have not found a solution. 我已经为这个简单的问题进行了大量搜索,但没有找到解决方案。 It looks really simple. 看起来真的很简单。 I have a dataframe with a column like this: 我有一个像这样的列的数据框:

Value
0.13
0.35
0.62
0.97
0.24
0.59
0.92
0.16
0.29
0.62
0.98

All values have a range between 0 and 1. What I want is that when the value starts to drop, I assign a new group to it. 所有值的范围都在0到1之间。我想要的是,当值开始下降时,我为其分配了一个新组。 Within each group, the value is increasing. 在每个组中,价值正在增加。 So the ideal outcome will look like this: 因此理想的结果将如下所示:

Value Group
0.13   1
0.35   1
0.62   1
0.97   1
0.24   2
0.59   2
0.92   2
0.16   3
0.29   3
0.62   3
0.98   3

Does anyone have a suggestion for how to address this? 有没有人建议如何解决这个问题?

This should do the trick, and uses only vectorised base functions. 这应该可以解决问题,并且仅使用向量化基本函数。 You may want to exchange the < for <= , if thats the behaviour you wanted. 如果这是您想要的行为,则可能需要将<换成<=

vec <- c(0.13, 0.35, 0.62, 0.97, 0.24, 0.59, 0.92, 0.16, 0.29, 0.62, 0.98)

cumsum(c(1, diff(vec) < 0))

This isn't the most elegant solution, but it works: 这不是最优雅的解决方案,但它可以工作:

value <- c(0.13, 0.35, 0.62, 0.97, 0.24, 0.59, 0.92, 0.16, 0.29, 0.62, 0.98)

foo <- data.frame(value, group = 1)
current_group <- 1
for(i in 2:nrow(foo)){
  if(foo$value[i] >= foo$value[i-1]){
    foo$group[i] <- current_group
  }else{
    current_group <- current_group + 1
    foo$group[i] <- current_group
  }
}


df <- data.frame( x = c(0.13, 0.35, 0.62, 0.97, 0.24, 0.59, 0.92, 0.16, 0.29, 0.62, 0.98))
df$y <- c(df$x[-1], NA)  # lag column
df$chgdir <- as.numeric(df$y - df$x < 0)  # test for change in direction
df$chgdir[is.na(df$chgdir)] <- 0  # deal with NA
df$group <- cumsum(df$chgdir) + 1  # determine group number
df[,c("x", "group")]
#>       x group
#> 1  0.13     1
#> 2  0.35     1
#> 3  0.62     1
#> 4  0.97     2
#> 5  0.24     2
#> 6  0.59     2
#> 7  0.92     3
#> 8  0.16     3
#> 9  0.29     3
#> 10 0.62     3
#> 11 0.98     3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM