我有一系列媒體資源,我必須爲其指定縣名。對於只有一個縣分配的特定來源(例如本地報紙),這非常簡單 - 我根據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
:
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次&相關w/Lehigh,費城,蒙哥馬利,&默瑟縣。與雅虎新聞有「NA」縣的記錄消失。在我的實際數據集中,我有大約100個縣,所以雅虎新聞&其相關變量(例如Score,Date,Author等 - 我總共有大約60個變量)將被複制100次。我還希望縣的這些新「重複」記錄被分配到County.Name變量中,我使用上面的switch
函數創建了這個變量。我不想要2個縣名字段,我想要所有這些新創建的縣下County.Names。
如果您可以向我們提供一些樣本數據並顯示期望的結果,那就太好了。 –
我想你可能正在尋找'merge',但是如果沒有更好的數據表示,很難說。 – Roland
對不起,它已經很晚了,我累了。更新更多的解釋和輸出讀數重現性。 – NiuBiBang