我有一个包含此数据的数据框和10列
ID | sequence | modification| ... | nºproject
DAT | atggggg | NULL | ... | project
DAT | atggggg | 7.UN | ... | project
DAT | actgat | NULL | ... | project
DAT | atgtagtt | NULL | ... | project
DAT | ttttaaat | 8.UN | ... | project
DAT | tatatccc | NULL | ... | project
DAT | atagattg | 9.AT | ... | project
DAT | atatagag | NULL | ... | project
DAT | gggatgac | NULL | ... | project
我一直在使用此代码来查找重复项。
data_table <- data.table(new_data_frame_PEP$sequence, new_data_frame_PEP$modifications)
colnames(data_table) <- c("sequence","modifications")
data_duplicate <- data_table[sequence %in% data_table[duplicated(data_table$sequence),]$sequence]
然而,我的问题是我有一些重复的序列,但有不同的修改。例如,在示例中:
DAT | atggggg | NULL | ... | project
DAT | atggggg | 7.UN | ... | project
使用“序列”列和“修改”列有没有办法在重复功能中使用两个条件?
答案 0 :(得分:1)
如果new_data_frame_PEP
是数据框,并且您想要检索sequence
中包含重复项的行,则可以改为使用:
res <- new_data_frame_PEP[duplicated(new_data_frame_PEP$sequence) |
duplicated(new_data_frame_PEP$sequence, fromLast=TRUE),]
在这里,我们拨打duplicated
两次:首先从sequence
列的开头到结尾,然后从结尾回到开始使用fromLast
。 duplicated
返回一个或者(即|
)的逻辑,以便我们检索所有具有重复项的行索引。然后,我们将new_data_frame_PEP
的行与这些行进行对。
要使用多个列完成相同操作,以便我们在sequence
和modification
列中找到所有具有重复值的行,我们需要选择要传递给{{1的列}}。这可以使用duplicated
:
subset
为了说明,我们创建了一个您发布的数据集,但我们只包含seq.mod <- subset(new_data_frame_PEP, select=c("sequence","modification"))
data_duplicate <- new_data_frame_PEP[duplicated(seq.mod) | duplicated(seq.mod, fromLast=TRUE),]
,ID
,sequence
和modification
列。另外,我们复制第一行,以便我们实际上在n_project
和sequence
中都有重复项:
modification
仅使用new_data_frame_PEP <- structure(list(ID = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L), .Label = "DAT ", class = "factor"), sequence = structure(c(4L,
4L, 4L, 1L, 5L, 8L, 7L, 2L, 3L, 6L), .Label = c(" actgat ",
" atagattg ", " atatagag ", " atggggg ", " atgtagtt ", " gggatgac ",
" tatatccc ", " ttttaaat "), class = "factor"), modification = structure(c(4L,
4L, 1L, 4L, 4L, 2L, 4L, 3L, 4L, 4L), .Label = c(" 7.UN ", " 8.UN ",
" 9.AT ", " NULL "), class = "factor"), n_project = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = " project ", class = "factor")), .Names = c("ID",
"sequence", "modification", "n_project"), class = "data.frame", row.names = c(NA,
-10L))
## ID sequence modification n_project
##1 DAT atggggg NULL project
##2 DAT atggggg NULL project
##3 DAT atggggg 7.UN project
##4 DAT actgat NULL project
##5 DAT atgtagtt NULL project
##6 DAT ttttaaat 8.UN project
##7 DAT tatatccc NULL project
##8 DAT atagattg 9.AT project
##9 DAT atatagag NULL project
##10 DAT gggatgac NULL project
,我们得到:
sequence
同时使用seq.only <- subset(new_data_frame_PEP, select=c("sequence"))
data_duplicate <- new_data_frame_PEP[duplicated(seq.only) | duplicated(seq.only, fromLast=TRUE),]
## ID sequence modification n_project
##1 DAT atggggg NULL project
##2 DAT atggggg NULL project
##3 DAT atggggg 7.UN project
和sequence
:
modification