增加一个值,如果一行中的数字发生变化。

huangapple go评论97阅读模式
英文:

Increase a value if a number in a row changes

问题

我尝试使用mutate()来增加一列中的值,如果另一行中的值发生变化,则重置为1,就像以下示例一样:

  1. col1 col2 count
  2. 0 1 1
  3. 0 1 1
  4. 0 2 2
  5. 0 3 3
  6. 1 4 1
  7. 1 5 2
  8. 1 5 2

在第一部分中,当row1中的值变化时,它运行良好,但第二部分中row2中的变化值不起作用。我只获得以下结果:

  1. col1 col2 count
  2. 0 1 1
  3. 0 1 2
  4. 0 2 3
  5. 0 3 4
  6. 1 4 1
  7. 1 5 2
  8. 1 5 3

这是我的有效代码:

  1. df1 <- df %>%
  2. group_by(col1, col2) %>%
  3. mutate(counter = row_number()) %>%
  4. ungroup

我已经尝试过以下代码:

  1. df1 <- df %>%
  2. group_by(col1) %>%
  3. mutate(counter = row_number()) %>%
  4. group_by(col2) %>%
  5. mutate(counter = 'failed_code') %>%
  6. ungroup

但是使用if_elsecase_when等函数与给定的参数无法工作。我如何实现对col2的计数器,仅在行发生变化时增加,如果col1变化,则重置为1

英文:

I'm trying to use mutate() to increase a value in a column if a value changes in another row and resets to 1 if a value changes in a third row like the following example:

  1. col1 col2 count
  2. 0 1 1
  3. 0 1 1
  4. 0 2 2
  5. 0 3 3
  6. 1 4 1
  7. 1 5 2
  8. 1 5 2

The part with changes in row1 works well but the second part with the changing values in row2 didn't work. I only get the following results:

  1. col1 col2 count
  2. 0 1 1
  3. 0 1 2
  4. 0 2 3
  5. 0 3 4
  6. 1 4 1
  7. 1 5 2
  8. 1 5 3

This is my working code:

  1. df1 &lt;- df %&gt;%
  2. group_by(col1, col2)%&gt;%
  3. mutate(counter=row_number())%&gt;%
  4. ungroup

I already tried this:

  1. df1 &lt;- df %&gt;%
  2. group_by(col1)%&gt;%
  3. mutate(counter=row_number())%&gt;%
  4. group_by(col2)%&gt;%
  5. mutate(counter= &#39;failed_code&#39;)%&gt;%
  6. ungroup

but using functions like if_else or case_when didn't work with my given arguments. How could I implement a counter for col2 which increases only if the rows changes and reset to 1 if col1 changes?

答案1

得分: 4

使用consecutive_id(在dplyr >= 1.1.0中引入)可以这样做:

  1. library(dplyr, warn=FALSE)
  2. dat <- data.frame(
  3. col1 = c(0, 0, 0, 0, 1, 1, 1),
  4. col2 = c(1, 1, 2, 3, 4, 5, 5)
  5. )
  6. dat |>
  7. mutate(count = consecutive_id(col2), .by = col1)
  8. #> col1 col2 count
  9. #> 1 0 1 1
  10. #> 2 0 1 1
  11. #> 3 0 2 2
  12. #> 4 0 3 3
  13. #> 5 1 4 1
  14. #> 6 1 5 2
  15. #> 7 1 5 2

请注意,我只翻译了代码部分,不包括注释和输出。

英文:

Using consecutive_id (introduced with dplyr &gt;= 1.1.0) you could do:

  1. library(dplyr, warn=FALSE)
  2. dat &lt;- data.frame(
  3. col1 = c(0, 0, 0, 0, 1, 1, 1),
  4. col2 = c(1, 1, 2, 3, 4, 5, 5)
  5. )
  6. dat |&gt;
  7. mutate(count = consecutive_id(col2), .by = col1)
  8. #&gt; col1 col2 count
  9. #&gt; 1 0 1 1
  10. #&gt; 2 0 1 1
  11. #&gt; 3 0 2 2
  12. #&gt; 4 0 3 3
  13. #&gt; 5 1 4 1
  14. #&gt; 6 1 5 2
  15. #&gt; 7 1 5 2

答案2

得分: 2

使用 data.table 你可以使用 rleid

  1. df <- structure(list(col1 = c(0L, 0L, 0L, 0L, 1L, 1L, 1L), col2 = c(1L,
  2. 1L, 2L, 3L, 4L, 5L, 5L)), class = "data.frame", row.names = c(NA,
  3. -7L))
  4. require(data.table)
  5. setDT(df)
  6. df[,count:=rleid(col2), by = col1]
  7. df
  8. # col1 col2 count
  9. #1: 0 1 1
  10. #2: 0 1 1
  11. #3: 0 2 2
  12. #4: 0 3 3
  13. #5: 1 4 1
  14. #6: 1 5 2
  15. #7: 1 5 2
英文:

With data.table you can use rleid:

  1. df &lt;- structure(list(col1 = c(0L, 0L, 0L, 0L, 1L, 1L, 1L), col2 = c(1L,
  2. 1L, 2L, 3L, 4L, 5L, 5L)), class = &quot;data.frame&quot;, row.names = c(NA,
  3. -7L))
  4. require(data.table)
  5. setDT(df)
  6. df[,count:=rleid(col2), by = col1]
  7. df
  8. # col1 col2 count
  9. #1: 0 1 1
  10. #2: 0 1 1
  11. #3: 0 2 2
  12. #4: 0 3 3
  13. #5: 1 4 1
  14. #6: 1 5 2
  15. #7: 1 5 2

huangapple
  • 本文由 发表于 2023年6月5日 23:59:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76408161.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定