R 如何根据另一列(在数据集中)中的条目删除列重复项

R 如何根据另一列(在数据集中)中的条目删除列重复项,r,duplicates,duplicate-removal,duplicate-data,R,Duplicates,Duplicate Removal,Duplicate Data,我有一个数据集,看起来像: ColA ColB ColC ColD ColE rs778 C Can + C/T rs778 C Pro + C/T rs779 P Can + A/G rs779 P Can - A/G 我想在C列的基础上删除A列中的重复条目。换句话说,如果A列中的两个条目相同,我希望保留的行由C列中的条目确定。如果C列中的条目相同,则保留的行应由

我有一个数据集,看起来像:

    ColA  ColB   ColC  ColD  ColE
    rs778   C   Can     +   C/T
    rs778   C   Pro     +   C/T
    rs779   P   Can     +   A/G
    rs779   P   Can     -   A/G
我想在C列的基础上删除A列中的重复条目。换句话说,如果A列中的两个条目相同,我希望保留的行由C列中的条目确定。如果C列中的条目相同,则保留的行应由D列确定。如果“Can”>“Pro”和“+”>“-”,然后我要寻找的最终输出将如下所示:

    ColA   ColB ColC   ColD ColE
    rs778   C   Can     +   C/T
    rs779   P   Can     +   A/G
我使用以下方法删除了完全重复的数据:

data2 <- data[!duplicated(data[-2]),]

data2这里有一个解决方案可以满足您的需要,但可能不是最优雅的方法

data = read.table(header=TRUE, stringsAsFactors=FALSE,
                  text="ColA  ColB   ColC  ColD  ColE
                        rs778   C   Can     +   C/T
                        rs778   C   Pro     +   C/T
                        rs779   P   Can     +   A/G
                        rs779   P   Can     -   A/G")

# Convert ColC and ColD to factors, controlling sort order with levels arg.
# "Can" will sort before "Pro", and "+" will sort before "-".
data$ColC = factor(data$ColC, levels=c("Can", "Pro"))
data$ColD = factor(data$ColD, levels=c("+", "-"))

# Sort rows.
data = data[order(data$ColA, data$ColC, data$ColD), ]

# Works because prefered ColA duplicate sorts highest.
data2 = data[!duplicated(data$ColA), ]

data2
#    ColA ColB ColC ColD ColE
# 1 rs778    C  Can    +  C/T
# 3 rs779    P  Can    +  A/G

这里有一个解决方案可以满足您的需要,但可能不是最优雅的方法

data = read.table(header=TRUE, stringsAsFactors=FALSE,
                  text="ColA  ColB   ColC  ColD  ColE
                        rs778   C   Can     +   C/T
                        rs778   C   Pro     +   C/T
                        rs779   P   Can     +   A/G
                        rs779   P   Can     -   A/G")

# Convert ColC and ColD to factors, controlling sort order with levels arg.
# "Can" will sort before "Pro", and "+" will sort before "-".
data$ColC = factor(data$ColC, levels=c("Can", "Pro"))
data$ColD = factor(data$ColD, levels=c("+", "-"))

# Sort rows.
data = data[order(data$ColA, data$ColC, data$ColD), ]

# Works because prefered ColA duplicate sorts highest.
data2 = data[!duplicated(data$ColA), ]

data2
#    ColA ColB ColC ColD ColE
# 1 rs778    C  Can    +  C/T
# 3 rs779    P  Can    +  A/G

非常感谢。在这种情况下,功能胜过优雅。:]非常感谢。在这种情况下,功能胜过优雅。:]