根据新数据框过滤数据框

时间:2017-04-26 13:08:53

标签: r

这是一些玩具数据

x <- c("Bird","Bird","Tiger","Bird","Fish","grey","blue","orange","green","yellow","10","5","7","12","5","10","10","8","12","2","10","20","10","18","3")
m <- matrix(x,byrow = F,ncol = 5,nrow= 5)
m <- as.data.frame(m)
colnames(m) <- c("Animal","colour","length","height","weight")

y <- c("Tiger","Bird","Bird","colour","length","colour","orange","10","green","orange/black","12","light green")
new.m <- matrix(y,byrow=F,ncol=4,nrow = 3)
new.m <- as.data.frame(new.m)
colnames(new.m) <- c("Animal","attribute","value","new value")

如何使用数据框m有效地更新new.m中的值。最终结果应如下所示:

z <- c("Bird","Bird","Tiger","Bird","Fish","grey","blue","orange/black"," light green","yellow","12","5","7","12","5","10","10","8","12","2","10","20","10","18","3")
update.m <- matrix(z,byrow = F,ncol = 5,nrow= 5)
update.m <- as.data.frame(update.m)
colnames(update.m) <- c("Animal","colour","length","height","weight")

对于new.m中的固定行,我可以轻松实现此目的。但这可以通过综合而非基于行的方式来完成吗?

1 个答案:

答案 0 :(得分:3)

通过基础R的一个想法。我们首先创建一个矩阵,其中包含我们需要更新值的列。我们使用match来更新值。 nomath条目生成NA,我们replace包含原始值并将其放回原始数据框中。

m3 <- sapply(m[c(2:3)], function(i) new.m$`new value`[match(i, new.m$value)])
m[c(2:3)] <- replace(m3, is.na(m3), m[c(2,3)][which(is.na(m3), arr.ind = TRUE)])

m
#  Animal       colour length height weight
#1   Bird         grey     12     10     10
#2   Bird         blue      5     10     20
#3  Tiger orange/black      7      8     10
#4   Bird  light green     12     12     18
#5   Fish       yellow      5      2      3