2014-09-29 1 views
0

데이터 세트를 정리하고 정렬했지만 불행히도 누군가가 데이터를 두 번 넣었습니다. R에서 데이터 프레임처럼 중복 된 서브 세트를 읽은 다음 큰 데이터 프레임에서 빼서 중복을 제거하는 쉬운 방법이 있습니까?R에서 큰 정렬 된 것에서 작은 데이터 프레임을 빼는 쉬운 방법이 있습니까?

명확하게하기 위해 일부 데이터는 여러 번 발생하므로 중복되는 행이있는 것만으로는 데이터가 두 번 병합 된 더 작은 데이터 집합에 있음을 의미하지 않습니다.

+0

... 그래서 중복 행이 허용됩니다 ... 그래서 당신은 단지 다른 data.frame에있는 중복 행을 제거하고 싶습니까? – russellpierce

+0

예, 작은 데이터 프레임의 모든 행에 대해 큰 데이터 프레임에서 해당 행의 인스턴스 하나를 찾아 삭제하려고합니다. – Escher

답변

0

이것은 느리고 끔찍한 접근이지만 일을 끝내게됩니다.

#Setting up data which has the A row 3 times, the B row twice, and the C row 3 times 
whichLet <- c("A","A","A","B","B","C","C","C") 
A <-data.frame(Dog=1,Bob=2,Cat=3) 
B <-data.frame(Dog=2,Bob=1,Cat=3) 
C <- data.frame(Dog=3,Bob=2,Cat=1) 
someDuplicates <- do.call(rbind,sapply(whichLet,get,simplify=FALSE)) 
# Specifying to remove one of the A rows and one of the C rows, this is the same as 
toRemove <- do.call(rbind,sapply(c("A","C"),get,simplify=FALSE)) 
# we now have the data frames you mentioned 

while (nrow(toRemove) > 0) { #keep doing this until we've removed all the rows you specified 
    uniqueDF <- unique(someDuplicates) 
    duplicatedDF <- someDuplicates[duplicated(someDuplicates),] 
    # removing the first row of toRemove from uniqueDF 
    uniqueDF <- uniqueDF[!apply(apply(uniqueDF,1,function(x) {x==toRemove[1,]}),2,all),] 
    # merging the unique back with the duplicates 
    someDuplicates <- rbind(uniqueDF,duplicatedDF) 
    # removing the item just removed from toRemove 
    toRemove <- toRemove[-1,] 
} 

참고 :이 기능은 자동으로 실패 할 수 있습니다. 고유 한 XML에 존재하지 않는 행을 제거하면 고유 한 XML의 설정이 모두 유지되므로 계속 진행됩니다.

+0

대단히 감사합니다. 이 문제가 다시 발생하면 도움이 될 것입니다. 방금 만든 데이터 집합에 고유 한 데이터 집합 행 (큰 데이터 집합을 형성하기 위해 병합 된 다른 하위 집합에서는 찾을 수 없음)이 바로 가기라는 취지를 가지고 있다는 것을 깨달았습니다. 그래서 텍스트로 내보내고, bash 쉘에서'grep -vFf small.txt big.txt 'big2.txt; cat small.txt >> big2.txt; 정렬 big2.txt> big.txt; – Escher

관련 문제