替换不同数据集中的缺失值

时间:2016-08-17 09:46:19

标签: r dataframe

我有

dat1<- read.table (text='Prey, spec, offer AAA, 77, PRML AAB, 2, PMRT AAC, 8, TBKB AAD, 90, MYO9', header =T, sep =',')

> dat1
  Prey spec offer
1  AAA   77  PRML
2  AAB    2  PMRT
3  AAC    8  TBKB
4  AAD   90  MYO9

我有另一个缺少值的数据框,想要填充先前数据框中的缺失值

dat2<- read.table (text='Prey, spec, offer AAB, NA, PMRT AAC, 8, TBKB AAD, NA, MYO9', header =T, sep =',')

> dat2
  Prey spec offer
1  AAB   NA  PMRT
2  AAC    8  TBKB
3  AAD   NA  MYO9

我想要的输出

 Prey spec offer
1  AAB    2  PMRT
2  AAC    8  TBKB
3  AAD   90  MYO9

1 个答案:

答案 0 :(得分:1)

我们可以将matchis.na

一起使用
i1 <- is.na(dat2$spec)
dat2$spec[i1] <- dat1$spec[match(dat2$Prey[i1], dat1$Prey)]
dat2
#  Prey spec offer
#1  AAB    2  PMRT
#2  AAC    8  TBKB
#3  AAD   90  MYO9

数据

dat1 <- structure(list(Prey = c("AAA", "AAB", "AAC", "AAD"), spec = c(77L, 
2L, 8L, 90L), offer = c("PRML", "PMRT", "TBKB", "MYO9")), .Names = c("Prey", 
"spec", "offer"), class = "data.frame", row.names = c("1", "2", 
"3", "4"))

dat2 <- structure(list(Prey = c("AAB", "AAC", "AAD"), spec = c(NA, 8L, 
NA), offer = c("PMRT", "TBKB", "MYO9")), .Names = c("Prey", "spec", 
"offer"), class = "data.frame", row.names = c("1", "2", "3"))