2017-05-03 8 views
0

두 테이블, Catdata 및 ElementData가 있습니다. ElementData$MatchCatdata$categoryfiction 또는 suspense 또는 thriller이면 0을 반환하고 그렇지 않으면 0을 원합니다. 반복없이 어떻게하면됩니까?범주가 다른 파일에 저장되고 요소가 여러 범주를 가질 수있는 R 범주가 일치하는지 어떻게 확인합니까?

이 Catdata

Element Category 
abc123 thriller 
abc123 horror 
def456 fiction 
def456 suspense 
def456 thriller 
pqr789 romance 
pqr789 fiction 
xyz123 thriller 

으로부터 elementData이

Element      Match 
def456 
abc123 
xyz123 
pqr789 
+0

'% in %'즉'as.integer (ElementData $ Element % in % Catdata [, 1]) '를 사용할 수 있습니다. – akrun

+0

카테고리가 일치하는지 여부를 확인하는 대신 categoryData $ ElementID에 요소가 있는지 여부를 반환하지 않습니다. 소설, 스릴러 또는 서스펜스? – MyLeftS0ck

+0

나는 질문을 잘못 읽었다 고 생각한다. 아마도 – akrun

답변

0

당신이 Catdata$Element의 해당 값을 Catdata$category 1 있는 경우 값을 반환 ElementData$Match을 원하는 경우이 기준과 일치하는 다음 작업을 수행 할 수 있습니다 :

library(dplyr) 
## Create lookup table of all elements matching the criteria: 
matchData <- Catdata %>% 
     group_by(Element) %>% 
     dplyr::summarise(Match = (Category %in% 
       c("fiction", "suspense", "thriller")) %>% 
      any %>% as.integer) 

## Merge your second table with the lookup table 
ElementData <- ElementData %>% left_join(matchData, by = "Element") 
+0

을 수정 한 솔루션이 게시되었습니다. 고맙습니다! – MyLeftS0ck

+0

'ElementData <- ElementData %> % left_join (matchData, by = c ("DataElement"= "Element"))와 같은 조인에 그 이름을 지정하면됩니다. – ikop

+0

이것을 어떻게 편집 할 수 있는지 알고 있습니다. 한 카테고리가 일치하면 0.33, 두 개의 일치하는 경우 0.66, 세 개의 카테고리가 모두 일치하면 1을 반환합니다. – MyLeftS0ck