R中两个表之间的特定值子集替换

时间:2019-07-19 09:32:49

标签: r replace subset

我想用表2的b列中的值替换表1中的b值。如果,表1的a列中的值等于表2的a列中的值

我尝试过此方法,但它给我有关替换长度问题的错误

table1$b[is.na(match(table1$a, 
table2$a))] <- 
as.character(table2$b[match(table1$a, 
table2$a)])

这是我正在处理的数据的示例:

table1 <- data.frame (a = 
c("OP2775iia","OP2775iib","OP2958i_a","OP2958i_b","OP2958iia"),
                   b = c(COM,NA,NA,NA,COM))


table2 <- data.frame (a = c("OP2775iib","OP2958i_b"),
                      b = c(SAV,PAV))

我想要一个输出,其中table1条目'OP2775iib'和'OP2958i_b'各自的b列值分别更改为'SAV'和'PAV'。

1 个答案:

答案 0 :(得分:0)

以下是使用merge的选项:

table1 <- data.frame (a = c("OP2775iia","OP2775iib","OP2958i_a","OP2958i_b","OP2958iia"),
  b = c("COM",NA,NA,NA,"COM"), stringsAsFactors = F)

table2 <- data.frame (a = c("OP2775iib","OP2958i_b"),
  b = c("SAV","PAV"), stringsAsFactors = F)

names(table2) <- c("a", "b2")


(table3 <- merge(x = table1, y = table2, all.x = TRUE))
table3$bnew <- with(table3, ifelse(!is.na(b2), b2, b))
table3

#           a    b   b2 bnew
# 1 OP2775iia  COM <NA>  COM
# 2 OP2775iib <NA>  SAV  SAV
# 3 OP2958i_a <NA> <NA> <NA>
# 4 OP2958i_b <NA>  PAV  PAV
# 5 OP2958iia  COM <NA>  COM