0

我有一系列媒体来源,我必须为其分配县名。对于只有一个县分配的某些来源(例如当地报纸),这相当简单——我基于一个switch根据来源名称分配县名的函数创建了一个县名变量。样本:

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

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

但是我有想分配给数据集中所有县的来源(NPR、AP 等)。这实质上是复制任何来源为“国家”的记录,并将记录分配给我数据集中的每个县。

dput当前文件布局:

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
))

在当前文件 NPR、美联社和雅虎新闻没有关联县 (“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
))

在所需的布局中,我已将每个国家来源及其分数分配给数据集中的四个县中的每一个。例如,雅虎新闻的 1 分被复制了 4 次,并与 Lehigh、Philadelphia、Montgomery 和 Mercer 县相关联。并且雅虎新闻拥有“NA”县的记录消失了。在我的实际数据集中,我有大约 100 个县,因此雅虎新闻及其相关变量(例如分数、日期、作者等——我总共有大约 60 个变量)将被复制 100 次。我还希望将这些新“重复”记录的县分配给我使用switch上面的函数创建的 County.Name 变量。我不想要 2 个县名称字段,我想要 County.Names 下的所有这些新创建的县。

4

1 回答 1

1

如果我对您的理解正确,这可能是一种可能性:

# 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)

按照更新的问题进行编辑

# 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)

对于您问题的最后一部分,您可以只rbind输入国家来源src_cntCounty.Name或从中选择相关变量dd2

于 2013-08-03T09:49:49.793 回答