如何对data.frame列表应用条件重复删除?

时间:2016-12-28 17:55:25

标签: r dataframe duplicates

我有data.frame列表,需要应用非常具体的重复删除方法。我有理由对此data.frame列表使用特定的条件重复删除。但是,每个data.frame的重复删除条件是不同的。我想为第一个列表元素完成重复删除;对于第二个列表元素,我需要搜索出现两次以上的行(freq> 2),并且只保留一行;对于第三个列表元素,搜索出现三次以上的行(freq> 3),并在该data.frame中保留两行。我正在尝试为此数据操作任务获得更多编程,动态的解决方案。我试过我的镜头来获得很好的解决方案,但无法获得我想要的输出。我怎样才能轻松实现这一目标?任何方式更有效地完成此任务尊重我的具体输出?好吗?

可重复的data.frame:

myList <- list(
    bar= data.frame(start.pos=c(9,19,34,54,70,82,136,9,34,70,136,9,82,136),
                    end.pos=c(14,21,39,61,73,87,153,14,39,73,153,14,87,153),
                    pos.score=c(48,6,9,8,4,15,38,48,9,4,38,48,15,38)),
    cat = data.frame(start.pos=c(7,21,21,72,142,7,16,21,45,72,100,114,142,16,72,114),
                     end.pos=c(10,34,34,78,147,10,17,34,51,78,103,124,147,17,78,124),
                     pos.score=c(53,14,14,20,4,53,20,14,11,20,7,32,4,20,20,32)),
    foo= data.frame(start.pos=c(12,12,12,58,58,58,118,12,12,44,58,102,118,12,58,118),
                    end.pos=c(36,36,36,92,92,92,139,36,36,49,92,109,139,36,92,139),
                    pos.score=c(48,48,48,12,12,12,5,48,48,12,12,11,5,48,12,5))
)

因为myList是自定义函数的结果,所以data.frame无法分离。我正在寻求更多的程序化解决方案,以便为我的数据进行特定的重复删除。如果输入是data.frame列表,如何进行特定的重复删除?

我想要的输出如下:

expectedList <- list(
    bar= data.frame(start.pos=c(9,19,34,54,70,82,136),
                    end.pos=c(14,21,39,61,73,87,153),
                    pos.score=c(48,6,9,8,4,15,38)),
    cat= data.frame(start.pos=c(7,21,72,142,7,16,45,100,114,142,16,114),
                    end.pos=c(10,34,78,147,10,17,51,103,124,147,17,124),
                    pos.score=c(53,14,20,4,53,20,11,7,32,4,20,32)),
    foo= data.frame(start.pos=c(12,12,44,58,58,118,102,118,118),
                    end.pos=c(36,36,49,92,92,139,109,139,139),
                    pos.score=c(48,48,12,12,12,5,11,5,5))
)

修改

在第二个data.frame cat中,我将查找出现三次的行,并将这些行仅保留一次;如果行出现两次,我就不会重复删除。

对于第三个data.frame foo,我将检查出现三次以上的行,并保留两个相同的行。这就是我试图为每个data.frame进行非常具体的重复删除。我如何获得输出?

如何获得所需的data.frame列表?我怎样才能轻松实现这一目标?非常感谢!

3 个答案:

答案 0 :(得分:5)

我们可以执行此Map来根据使用向量(list)中指定的相应数字创建的逻辑索引对1:3元素的行进行子集化。将data.frame中的list元素转换为data.tablesetDT(x)),按列分组(&#39; start.pos&#39;,&#39; end。 pos&#39;,&#39; pos.score&#39;),我们得到行数(.N),用if/else创建一个逻辑索引并得到满足在OP的帖子中指定的条件,使用.I获取行索引,提取索引列($V1)并使用它来对数据集进行子集化。

library(data.table)
res <- Map(function(x,y) setDT(x)[x[,  .I[if(.N > y) seq_len(pmax(y-1, 1)) 
        else seq_len(.N)]  , .(start.pos, end.pos, pos.score)]$V1], myList, 1:3)
sapply(res, nrow)
#bar cat foo 
#  7  12   9 

sapply(expectedList, nrow) 
#bar cat foo 
#7  12   9 

答案 1 :(得分:1)

将以下函数应用于列表的每个数据框,指定每行的最大频率

removeDuplicate = function(df, freq=1) {

    # back up the dataframe and add a row id
    tmp = df;
    tmp$cnt = 1:NROW(df);
    # get each row frequency
    cnt = aggregate(cnt~., tmp, length);

    # merge the original data-frame and the row-frequency data-frame
    tmp = merge(df, cnt, by=names(df));
    tmp = rbind(
                tmp[tmp$cnt<=freq, names(df)], # keep all the rows which frequency is not greater than the max allowed
                cnt[, names(df)] # add all the other rows just once
            );

    return(tmp);

}

要将函数应用于每个数据框,我会这样做:

expectedList = myList
maxFreq = c(1, 2, 3)
for(i in 1:length(expectedList)) {

    expectedList[[i]] = removeDuplicate(expectedList[[i]], maxFreq[i])

}

但我认为可以找到使用lapply的更优雅的解决方案......

答案 2 :(得分:1)

# Separate individual dataframes
bar = myList$bar 
cat = myList$cat
foo = myList$foo

# We will need ddply command of plyr package
library(plyr)

#Count how many times the rows have repeated and put the value in the fourth column (V1)
bar = ddply(bar,.(start.pos,end.pos,pos.score),nrow)
cat = ddply(cat,.(start.pos,end.pos,pos.score),nrow)
foo = ddply(foo,.(start.pos,end.pos,pos.score),nrow)

# For each data.frame, change the number of repetions to appropriate number of times
# if the rows have repeated for more than the desired number of times
# i.e 1 for bar, 2 for cat, and 3 for foo
for (i in 1:nrow(bar)){
if (bar$V1[i] > 1){
bar$V1[i] = 1
}}
for (i in 1:nrow(cat)){
if (cat$V1[i] > 2){
cat$V1[i] = 1
}}
for (i in 1:nrow(foo)){
if (foo$V1[i] > 2){
foo$V1[i] = 2
}}

# Repeat each row for the number of times indicated in the fourth column.
# This will be 1 for bar, up to 2 for cat, and up to 3 for foo
bar = bar[rep(row.names(bar), bar[,4]), 1:3]
cat = cat[rep(row.names(cat), cat[,4]), 1:3]
foo = foo[rep(row.names(foo), foo[,4]), 1:3]

# Set the rownames to NULL if desired
rownames(cat) = NULL
rownames(bar) = NULL
rownames(foo) = NULL

# Combine the indivudal data.frames into a new list
expectedList = list(bar = bar,cat = cat,foo = foo)