2016-09-26 118 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) 
相關問題