[英]filter data frame based on new data frame
Here is some toy data 这是一些玩具数据
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")
How can I efficiently update the values in m
using the data frame new.m
. 如何使用数据框new.m
有效地更新m
的值。 The final result should look like this: 最终结果应如下所示:
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")
For a fixed row in new.m I can achieve this easily. 对于new.m中的固定行,我可以轻松实现。 But can this be done in comprehensive not row based way? 但这可以通过综合而不是基于行的方式来完成吗?
One idea via base R. We first create a matrix with the columns we need to update the values. 一个通过基数R的想法。我们首先创建一个矩阵,其中包含我们需要更新值的列。 We use match
to update the values. 我们使用match
更新值。 The nomath
entries result to NA
which we replace
with original values and put them back in the original data frame. nomath
项的结果为NA
,我们replace
其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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.