我事先为这个冗长的事道道歉。
我的数据集包含MID dyads(1800-2001),例如
id cc1 year cap1
4114 2 1994 .
4113 2 1994 .
4113 2 1996 .
另一个数据集的每个县代码年(从1800-2001)有cap1分数,例如
cc1 year CINC
2 1816 0.039
2 1817 0.035
2 1818 0.036
我想使用第二个数据集中的CINC值来填充数据集中cap1变量中的缺失值。
要明确的是,我还应该添加的问题是每个启动器年份可以超过1个或没有MID。因此,例如,我的数据集将对cap1变量进行2680次观察。但是,第二个数据集有14199个观测值。
我在stackoverflow上搜索论坛并咨询了一位朋友并提出了以下内容
mydata$cap1=mydata1[mydata1$ccode==mydata$cc1 & mydata1$year==mydata$year,]$cinc
我的mydata是我的数据集,而mydata1是第二个数据集
返回此错误
$<-.data.frame
中的错误(*tmp*
,&#34; cap1&#34;,值= c(0.0396975,0.0358166,: 替换有3行,数据有2680另外:警告信息:
1:在mydata1 $ ccode == mydata $ cc1: 较长的物体长度不是较短物体长度的倍数
2:在mydata1 $ year == mydata $ year: 较长的物体长度不是较短物体长度的倍数
修改
我的数据集
structure(list(idnum = c(4054L, 4186L, 4206L, 4273L, 2589L, 2587L
), cc1 = c(365L, 2L, 640L, 2L, 541L, 630L), cap1 = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_)), .Names = c("idnum",
"cc1", "cap1"), datalabel = "", time.stamp = "29 Nov 2015 23:36", formats = c("%8.0g",
"%8.0g", "%9.0g"), types = c(65529L, 65529L, 65527L), val.labels = c("",
"", ""), var.labels = c("MID ID number", "Initiator country code",
"Initiator's capabilities"), version = 117L, label.table = list(), expansion.fields = list(), strl = list(), byteorder = "LSF", row.names = 2675:2680, class = "data.frame")
第二个数据集
structure(list(stateabb = structure(c(208L, 208L, 208L, 208L,
208L, 208L), .Label = c("AAB", "AFG", "ALB", "ALG", "AND", "ANG",
"ARG", "ARM", "AUH", "AUL", "AUS", "AZE", "BAD", "BAH", "BAR",
"BAV", "BEL", "BEN", "BFO", "BHM", "BHU", "BLR", "BLZ", "BNG",
"BOL", "BOS", "BOT", "BRA", "BRU", "BUI", "BUL", "CAM", "CAN",
"CAO", "CAP", "CDI", "CEN", "CHA", "CHL", "CHN", "COL", "COM",
"CON", "COS", "CRO", "CUB", "CYP", "CZE", "CZR", "DEN", "DJI",
"DMA", "DOM", "DRC", "DRV", "ECU", "EGY", "EQG", "ERI", "EST",
"ETH", "ETM", "FIJ", "FIN", "FRN", "FSM", "GAB", "GAM", "GDR",
"GFR", "GHA", "GMY", "GNB", "GRC", "GRG", "GRN", "GUA", "GUI",
"GUY", "HAI", "HAN", "HON", "HSE", "HSG", "HUN", "ICE", "IND",
"INS", "IRE", "IRN", "IRQ", "ISR", "ITA", "JAM", "JOR", "JPN",
"KEN", "KIR", "KOR", "KUW", "KYR", "KZK", "LAO", "LAT", "LBR",
"LEB", "LES", "LIB", "LIE", "LIT", "LUX", "MAA", "MAC", "MAD",
"MAG", "MAL", "MAS", "MAW", "MEC", "MEX", "MLD", "MLI", "MLT",
"MNC", "MNG", "MOD", "MON", "MOR", "MSI", "MYA", "MZM", "NAM",
"NAU", "NEP", "NEW", "NIC", "NIG", "NIR", "NOR", "NTH", "OMA",
"PAK", "PAL", "PAN", "PAP", "PAR", "PER", "PHI", "PMA", "PNG",
"POL", "POR", "PRK", "QAT", "ROK", "ROM", "RUS", "RVN", "RWA",
"SAF", "SAL", "SAU", "SAX", "SEN", "SEY", "SIC", "SIE", "SIN",
"SKN", "SLO", "SLU", "SLV", "SNM", "SOL", "SOM", "SPN", "SRI",
"STP", "SUD", "SUR", "SVG", "SWA", "SWD", "SWZ", "SYR", "TAJ",
"TAW", "TAZ", "THI", "TKM", "TOG", "TON", "TRI", "TUN", "TUR",
"TUS", "TUV", "UAE", "UGA", "UKG", "UKR", "URU", "USA", "UZB",
"VAN", "VEN", "WRT", "WSM", "YAR", "YEM", "YPR", "YUG", "ZAM",
"ZAN", "ZIM"), class = "factor"), ccode = c(990L, 990L, 990L,
990L, 990L, 990L), year = 2002:2007, irst = c(0L, 0L, 0L, 0L,
0L, 0L), milex = c(0L, 0L, 0L, 0L, 0L, 0L), milper = c(0L, 0L,
0L, 0L, 0L, 0L), pec = c(47.0876, 44.54492, 42.7761, 43.50082,
44.62415, 44.93124), tpop = c(178L, 180L, 182L, 183L, 185L, 187L
), upop = c(0L, 0L, 0L, 0L, 0L, 0L), cinc = c(5.12e-06, 5.08e-06,
5.05e-06, 5.01e-06, 5.01e-06, 4.99e-06), version = c(4L, 4L,
4L, 4L, 4L, 4L)), .Names = c("stateabb", "ccode", "year", "irst",
"milex", "milper", "pec", "tpop", "upop", "cinc", "version"), row.names = 14194:14199, class = "data.frame")
我的尝试
require(foriegn)
require(readstata13)
mydata=read.dta13("Schultz-Geddes.dta")
mydata1=read.csv("NMC_v4_0-3.csv")
mydata$cap1=mydata1[mydata1$ccode==mydata$cc1 & mydata1$year==mydata$year,]$cinc
install.packages("dplyr")
library(dplyr)
joined <- mydata %>%
left_join(mydata1, c("cc1", "year"))
答案 0 :(得分:1)
您想要将两个数据集连接在一起。
library(dplyr)
joined <- your_dataset %>%
inner_join(the_other_dataset, c("cc1", "year"))
(从您的问题中不完全清楚数据集的内容是什么,因此您可能需要left_join
而不是inner_join
。您还可以使用merge
基-R)。
然后,您可以将cap1
列更改为在缺少时使用CICN
joined %>%
mutate_(cap1 = ~ ifelse(is.na(cap1), CICN, cap1))