[英]Replace values in column of a dataframe when matching to column in another dataframe in R
[英]R - Add a new column to a dataframe using matching values of another dataframe
merge(table1, table2[, c("pid", "val2")], by="pid")
添加all.x=TRUE
参数以保留 table1 中所有在 table2 中没有匹配项的 pid...
你走在正确的轨道上。 这是使用匹配的一种方法...
table1$val2 <- table2$val2[match(table1$pid, table2$pid)]
我不确定你是不是这个意思,但你可能会使用:
newtable <- merge(table1,table2, by = "pid")
这将创建一个名为 newtable 的新表,其中包含 3 列和与 id 匹配的值,在本例中为“pid”。
我来晚了,但以防万一其他人问同样的问题:
这正是 dplyr 的 inner_merge 所做的。
table1.df <- dplyr::inner_join(table1, table2, by=pid)
by-command 指定应该使用哪个列来匹配行。
编辑:我曾经很难记住它是 [join],而不是 [merge]。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.