增加按组重复数字的列

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

Add column with repeated numbers per group

问题

我想要在数据框中添加一个新的列,其中每个组(Chr)的末尾都重复1,1,1,2,2,2,3,3,3,如果所有的组都能被3整除,那将很容易实现,但是当组长度能被2整除时,我不确定最后一个重复会发生什么。

original <- data.frame(Chr=c("chr1","chr1","chr1","chr1","chr1","chr2","chr2","chr2","chr2","chr2","chr3"),
value=c(1,3,1,3,5,6,3,1,3,5,0),
seq=c(1,2,3,4,5,1,2,3,4,5,6))

modified <- data.frame(Chr=c("chr1","chr1","chr1","chr1","chr1","chr2","chr2","chr2","chr2","chr2","chr3"),
value=c(1,3,1,3,5,6,3,1,3,5,0),
seq=c(1,2,3,4,5,1,2,3,4,5,6),
rep=c(1,1,1,2,2,1,1,1,2,2,1))

英文:

I want to add a new column with a 3-number repeat 1,1,1,2,2,2,3,3,3 until the end of each group (Chr) within the data frame. This would be easy if all the groups can be divided by 3, but I am not sure how to do this when the group length is divisible by 2. What happens with the last repeat within each group?

  1. original &lt;- data.frame(Chr=c(&quot;chr1&quot;,&quot;chr1&quot;,&quot;chr1&quot;,&quot;chr1&quot;,&quot;chr1&quot;,&quot;chr2&quot;,&quot;chr2&quot;,&quot;chr2&quot;,&quot;chr2&quot;,&quot;chr2&quot;,&quot;chr3&quot;),
  2. value=c(1,3,1,3,5,6,3,1,3,5,0),
  3. seq=c(1,2,3,4,5,1,2,3,4,5,6))
  4. modified &lt;- data.frame(Chr=c(&quot;chr1&quot;,&quot;chr1&quot;,&quot;chr1&quot;,&quot;chr1&quot;,&quot;chr1&quot;,&quot;chr2&quot;,&quot;chr2&quot;,&quot;chr2&quot;,&quot;chr2&quot;,&quot;chr2&quot;,&quot;chr3&quot;),
  5. value=c(1,3,1,3,5,6,3,1,3,5,0),
  6. seq=c(1,2,3,4,5,1,2,3,4,5,6),
  7. rep=c(1,1,1,2,2,1,1,1,2,2,1))

答案1

得分: 0

我们可以使用 gl() 函数:

  1. library(dplyr)
  2. original %>%
  3. mutate(rep = as.integer(gl(n(), 3, n())), .by = Chr)
  1. Chr value seq group
  2. 1 chr1 1 1 1
  3. 2 chr1 3 2 1
  4. 3 chr1 1 3 1
  5. 4 chr1 3 4 2
  6. 5 chr1 5 5 2
  7. 6 chr2 6 1 1
  8. 7 chr2 3 2 1
  9. 8 chr2 1 3 1
  10. 9 chr2 3 4 2
  11. 10 chr2 5 5 2
  12. 11 chr3 0 6 1
英文:

We could use gl() function:

  1. library(dplyr)
  2. original %&gt;%
  3. mutate(rep = as.integer(gl(n(),3,n())), .by=Chr)
  1. Chr value seq group
  2. 1 chr1 1 1 1
  3. 2 chr1 3 2 1
  4. 3 chr1 1 3 1
  5. 4 chr1 3 4 2
  6. 5 chr1 5 5 2
  7. 6 chr2 6 1 1
  8. 7 chr2 3 2 1
  9. 8 chr2 1 3 1
  10. 9 chr2 3 4 2
  11. 10 chr2 5 5 2
  12. 11 chr3 0 6 1

huangapple
  • 本文由 发表于 2023年2月19日 18:31:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75499479.html
匿名

发表评论

匿名网友

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

确定