2016-09-26 102 views
0

我有这样两组数据帧,我想通过自己的字符串加入一个字符串是否包含在另一个字符串

> part <- data.frame(name = c("I", "want", "to", "go", "there", "you", "are", "seeing"), 
+     value = c(0.77, 0.55, 0.33, 0.4, 0.5, 0.9, 1.0, 0.91)) 
> full <- data.frame(sentence = c("I want to go there", "you are seeing")) 

加入他们,我怎么能加入他们基于这样的句子的名字是存在?像这样

name value   sentence 
1  I 0.77 I want to go there 
2 want 0.55 I want to go there 
3  to 0.33 I want to go there 
4  go 0.40 I want to go there 
5 there 0.50 I want to go there 
6 you 0.90  you are seeing 
7 are 1.00  you are seeing 
8 seeing 0.91  you are seeing 
+1

我想你会需要一个您的数据框中的一列,用于划分一个句子结束位置和另一个句子结束位置。 –

回答

0

您可以

sapply(part$name, function(x) {grepl(full[1,1],pattern = x)}) 

和工作从那里开始:

工作,但也许不是有效的例子:

lst <- sapply(full$sentence,function(y){ 
which(sapply(part$name, function(x) {grepl(y,pattern = x)})) 
}) 


lst2 <- lapply(seq_along(lst),function(x) 
{data.frame(full=rep(full[x,1],length(lst[[x]])),part=part[lst[[x]],1])}) 

do.call(rbind, lst2) 
相关问题