英文:
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?
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))
答案1
得分: 0
我们可以使用 gl()
函数:
library(dplyr)
original %>%
mutate(rep = as.integer(gl(n(), 3, n())), .by = Chr)
Chr value seq group
1 chr1 1 1 1
2 chr1 3 2 1
3 chr1 1 3 1
4 chr1 3 4 2
5 chr1 5 5 2
6 chr2 6 1 1
7 chr2 3 2 1
8 chr2 1 3 1
9 chr2 3 4 2
10 chr2 5 5 2
11 chr3 0 6 1
英文:
We could use gl()
function:
library(dplyr)
original %>%
mutate(rep = as.integer(gl(n(),3,n())), .by=Chr)
Chr value seq group
1 chr1 1 1 1
2 chr1 3 2 1
3 chr1 1 3 1
4 chr1 3 4 2
5 chr1 5 5 2
6 chr2 6 1 1
7 chr2 3 2 1
8 chr2 1 3 1
9 chr2 3 4 2
10 chr2 5 5 2
11 chr3 0 6 1
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论