2013-08-03 3 views
0

나는 카운티 이름을 지정해야하는 일련의 미디어 소스를 가지고 있습니다. 단일 카운티 지정이있는 특정 소스 (예 : 지역 신문)의 경우 매우 간단했습니다. 소스 이름을 기준으로 카운티 이름을 할당 한 switch 함수를 기반으로 카운티 이름 변수를 만들었습니다. 샘플 :R : 중복 레코드 생성 및 지정

switchfun <- function(x) {switch(x, 'Morning Call' = 'Lehigh', 'Inquirer' =  
'Philadelphia', 'Daily Ledger' = 'Mercer', 'Null') } 

County.Name <- as.character(lapply(Source, switchfun)) 

하지만 데이터 세트의 모든 카운티에 할당 할 출처 (NPR, AP 등)가 있습니다. 이것은 본질적으로 소스가 "국가"인 모든 레코드를 복제하고 내 데이터 세트의 모든 카운티에 레코드를 할당합니다. 현재 파일 레이아웃의

dput : 현재 파일 NPR에서

structure(list(Source = structure(c(5L, 2L, 4L, 3L, 7L, 1L, 6L 
), .Label = c("Associated Press", "Daily Ledger", "Herald Tribune", 
"Inquirer", "Morning Call", "NPR", "Yahoo News"), class = "factor"), 
County = structure(c(1L, 2L, 4L, 3L, NA, NA, NA), .Label = c("Lehigh", 
"Mercer", "Montgomery", "Philadelphia"), class = "factor"), 
Score = c(3L, 10L, 4L, 8L, 1L, 3L, 6L)), .Names = c("Source", 
"County", "Score"), class = "data.frame", row.names = c(NA, -7L 
)) 

, AP 통신, & 야후 뉴스 연관된 카운티 ("NA")를 가지지 않습니다. 원하는 파일 레이아웃의

dput : 원하는 레이아웃에서

structure(list(Source = structure(c(5L, 2L, 4L, 3L, 7L, 7L, 7L, 
7L, 1L, 1L, 1L, 1L, 6L, 6L, 6L, 6L), .Label = c("Associated Press", 
"Daily Ledger", "Herald Tribune", "Inquirer", "Morning Call", 
"NPR", "Yahoo News"), class = "factor"), County = structure(c(1L, 
2L, 4L, 3L, 1L, 2L, 4L, 3L, 1L, 2L, 4L, 3L, 1L, 2L, 4L, 3L), .Label = c("Lehigh", 
"Mercer", "Montgomery", "Philadelphia"), class = "factor"), Score = c(3L, 
10L, 4L, 8L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 6L, 6L, 6L, 6L)), .Names = c("Source", 
"County", "Score"), class = "data.frame", row.names = c(NA, -16L 
)) 

, 나는 데이터 세트의 4 개 군의 각 각 국가 소스를 &의 점수를 부여했다. 예 : Yahoo News &은 1의 그것의 점수는 Lehigh, Philadelphia, Montgomery, & Mercer 군에 4 번 &와 연관됩니다. 카운티가 'NA'인 야후 뉴스의 기록은 사라집니다. 내 실제 데이터 세트에는 약 100 개 카운티가 있으므로 Yahoo! News &과 관련된 변수 (예 : 점수, 날짜, 작성자 등 - 약 60 개의 변수가 있음)가 100 번 복제됩니다. 또한 새로이 "복제 된"레코드의 카운티를 위의 switch 함수를 사용하여 만든 County.Name 변수에 할당해야합니다. 나는 2 개의 카운티 이름 필드가 필요 없으므로 새로 생성 된 모든 카운티를 County.Names 아래에두기를 바랍니다. 업데이트 된 질문 다음

# a (minimal) data frame with all unique source-county combinations 
src_cnt <- data.frame(source = c("Morning Call", "AP", "AP", "AP"), county = c("Lehigh", "Lehigh", "Mercer", "Phila")) 

# a data frame with a unique score for each source 
src_score <- data.frame(source = c("Morning Call", "AP"), score = c(10, 3)) 

merge(src_cnt, src_score) 

편집 : 내가 제대로 이해 한 경우

+5

샘플 데이터를 제공하고 원하는 결과를 표시 할 수 있다면 좋을 것입니다. –

+3

'병합'을 찾고 있을지 모르지만 데이터를보다 잘 표현하지 않으면 말하기가 어렵습니다. – Roland

+0

죄송합니다. 늦었고 피곤했습니다. 재현성을 위해 더 자세한 설명과'dput' 정보가 업데이트되었습니다. – NiuBiBang

답변

1

이 하나의 가능성 일 수있다

# Assuming your current data is named dd 
# select the national sources, i.e. the sources where County is missing 
src_national <- dd$Source[is.na(dd$County)]) 

# select unique counties 
counties <- unique(dd$County[!is.na(dd$County)]) 

# create all combinations of national sources and counties 
src_cnt <- expand.grid(Source = src_national, County = counties) 

# add score from current data to national sources 
src_cnt2 <- merge(src_cnt, dd[is.na(dd$County), c("Source", "Score")], by = "Source") 

# add national sources to local sources in dd 
dd2 <- rbind(dd[!is.na(dd$County), ], src_cnt2) 

# order by Sourcy and County 
# assuming desired data is named `desired` 
library(plyr) 
desired2 <- arrange(df = desired, Source, County) 
dd2 <- arrange(df = dd2, Source, County) 
all.equal(desired2, dd2) 

질문의 맨 마지막 부분에 대한 당신은 할 수 rbindsrc_cnt ~ County.Name의 국가 출처 또는 관련 변수를 dd2

에서 선택하십시오.
+0

실제로는 고유 한 ID도 고려해야하므로 다음과 같이 수정했습니다. 'src_cnt <-expand.grid (Source = src_natl $ Source, ID = src_natl $ ID, 카운티 = 카운티) # counties <-c ('카운티 1'... '카운티 120')'; (src_cnt, src_natl, by = c ("ID", "Source"))', src_cnt'를 원래 데이터 세트의 ID와 Source만으로 필터링했습니다. – NiuBiBang