[英]Compare names between columns in R
The data I have look more or less like this: 我所看到的数据或多或少是这样的:
data1 <- data.frame(col=c("Peter i.n.","Victor Today Morgan","Obelix","One More"))
data2 <- data.frame(num=c(123,434,545,11,22),col=c("Victor Today","Obelix Mobelix is.",
"Peter Asterix i.n.","Also","Here"))
I would like to match names across the two dataframes and get the column num
into data1. 我想在两个数据框中匹配名称,并将
num
列放入data1中。
Desired outcome: 期望的结果:
col num
1 Peter i.n. 545
2 Victor Today Morgan 123
3 Obelix 434
I have tried this, but doesn't work as expected. 我已经尝试过了,但是没有按预期工作。
filter <- sapply(as.character(data1$col), function(x) any(grepl(x,as.character(data2$col))))
data1$num <- data2[filter,]
firstName <- function(x) sub(" .*", "", x)
data1$num <- data2$num[match(firstName(data1$col), firstName(data2$col))]
data1[!is.na(data1$num),]
If you don't mind which col
names you wan to see ( data1
or data2
), you could utilizes your own solution by: 如果您不介意希望查看哪个
col
名( data1
或data2
),则可以通过以下方式利用自己的解决方案:
data2[as.logical(sapply(gsub(" .*", "", as.character(data2$col)), function(x) any(grepl(x, as.character(data1$col))))), ]
## num col
## 1 123 Victor Today
## 2 434 Obelix Mobelix is.
## 3 545 Peter Asterix i.n.
This will match the first word in data2$col
to data1$col
and retrieve the correct entries out of data2
这会将
data2$col
中的第一个单词与data1$col
匹配,并从data2
检索正确的条目
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.