2010-12-06 4 views
0

아래 예제에서는 First, Last 및 Address 1/2가 일치하는 두 개의 행이 있습니다. 두 가지 전자 메일 주소가 있지만 추가 사용자 정의 필드가 하나 있습니다. 내가 뭘하고 싶은지 체계적으로 첫 번째 행을 선택하면 여러 행이 표시됩니다. 즉다른 필드의 일치 및 NA를 기반으로 행 선택

Lines <- " 
First, Last, Address, Address 2, Email, Custom1, Custom2, Custom3 
A, B, C, D, [email protected],1,2,3 
A, B, C, D, [email protected],1,2, 
" 
con <- textConnection(Lines) 

:

마지막 & & 첫 번째 IF 주소 1 개 & 주소 2 경기, custom3가 NA없는 경우에는 하나의 것을 선택합니다.

큰 데이터 집합에 어떻게 적용할까요?

참고 : 속도는 중요하지 않지만 plyr을 사용한 예가 가장 유용합니다.

답변

1

는 여기에 plyr 해결책 :

con <- textConnection(Lines <- " 
First, Last, Address, Address 2, Email, Custom1, Custom2, Custom3 
A, B, C, D, [email protected],1,2,3 
A, B, C, D, [email protected],1,2, 
") 
x <- read.csv(con) 
close(con) 

ddply(x, .(First, Last, Address, Address.2), function(x2) x2[!is.na(x2$Custom3), c("Email", "Custom1", "Custom2", "Custom3")]) 
4
여기

일부 비 plyr의 예입니다, 내가 유용한 참을 수 있기 때문이다. ;-)

x <- read.csv(con) 
close(con) 

# Using split-apply-combine 
do.call(rbind, lapply(split(x, x[,1:4]), function(X) X[!is.na(X$Custom3),])) 

# Using ave 
x[!ave(x$Custom3, x[,1:4], FUN=is.na),] 
+1

비 plyr 예? 너 미쳤 니? – Shane

+1

@Shane 나는 트레일 믹스보다 너트니까. –

관련 문제