2017-10-11 63 views
2

我有两个数据帧, DF1:匹配排名最高的字在数据帧R列文本

df1 <- c("A large bunch of purple grapes", "large green potato sack", "small red tomatoes", "yellow and black bananas") 
df1 <- data.frame(df1) 

DF2:

Word <- c("green", "purple", "grapes", "small", "sack", "yellow", "bananas", "large) 

Rank <- c(20,18,22,16,15,17,6,12) 

df2 <- data.frame(Word,Rank) 

DF1:

ID  Sentence 
1  A large bunch of purple grapes 
2  large green potato sack 
3  small red tomatoes 
4  yellow and black bananas 

DF2:

ID  Word  Rank 
1  green  20 
2  purple  18 
3  grapes  22 
4  small  16 
5  Sack  15 
6  yellow  17 
7  bananas 6 
8  large  12 

我想要做的是;将df2中的单词与“Sentence”列中包含的单词相匹配,并在df1中插入一个包含df2中排名最高的匹配单词的新列。因此,像这样:

DF1:

ID  Sentence       Word 
1  A large bunch of purple grapes grapes 
2  large green potato sack   green 
3  small red tomatoes    small 
4  yellow and black bananas   yellow 

我最初用于下面的代码相匹配的话,当然这会创建一个包含所有匹配的单词列:

x <- sapply(df2$Word, function(x) grepl(tolower(x), tolower(df1$Sentence))) 

df1$top_match <- apply(x, 1, function(i) paste0(names(i)[i], collapse = " ")) 
+0

如果一个句子没有匹配'df2'的是,做你想做的只是返回'NA'任何文字?在这种情况下,所有的句子都有匹配,但我只是想确保你没有寻找更一般的东西。 – useR

+0

是的,返回N/A很好,谢谢! – Jammin

+0

另外,你能否提供你的数据为'deput(df1)'deput(df2)'或者你用来生成它们的代码? – useR

回答

0

这是tidyverse + stringr解决方案:

library(tidyverse) 
library(stringr) 

df1$Sentence %>% 
    str_split_fixed(" ", Inf) %>% 
    as.data.frame(stringsAsFactors = FALSE) %>% 
    cbind(ID = rownames(df1), .) %>% 
    gather(word_count, Word, -ID) %>% 
    inner_join(df2, by = "Word") %>% 
    group_by(ID) %>% 
    filter(Rank == max(Rank)) %>% 
    select(ID, Word) %>% 
    right_join(rownames_to_column(df1, "ID"), by = "ID") %>% 
    select(ID, Sentence, Word) 

结果:

# A tibble: 4 x 3 
# Groups: ID [4] 
    ID      Sentence Word 
    <chr>       <chr> <chr> 
1  1 A large bunch of purple grapes grapes 
2  2  large green potato sack green 
3  3    small red tomatoes small 
4  4  yellow and black bananas yellow 

注:

您可以忽略说,来自因子胁迫ID成字符的警告。我还修改了您的数据集,以包含df1的适当列名,并禁止自动强制角色转换为因素。

数据:

df1 <- c("A large bunch of purple grapes", "large green potato sack", "small red tomatoes", "yellow and black bananas") 
df1 <- data.frame(Sentence = df1, stringsAsFactors = FALSE) 

Word <- c("green", "purple", "grapes", "small", "sack", "yellow", "bananas", "large") 
Rank <- c(20,18,22,16,15,17,6,12) 
df2 <- data.frame(Word,Rank, stringsAsFactors = FALSE) 
+0

干杯,完美的工作!非常感谢! – Jammin

0

我已经写了一小段(但具有不同的变量名称)

> inp1 
    ID       Word new_word 
1 1  large green potato sack green 
2 2 A large bunch of purple grapes grapes 
3 3  yellow and black bananas yellow 
> 
> inp2 
    ID Word Rank 
1 1 green 20 
2 2 purple 18 
3 3 grapes 22 
4 4 small 16 
5 5 Sack 15 
6 6 yellow 17 
7 7 bananas 6 
8 8 large 12 
> 
> inp1$new_word <- lapply(inp1$Word, function(text){ inp2$Word[inp2$Rank == max(inp2$Rank[inp2$Word %in% unique(as.vector(str_match(text,inp2$Word)))])]}) 
> 
> inp1 
    ID       Word new_word 
1 1  large green potato sack green 
2 2 A large bunch of purple grapes grapes 
3 3  yellow and black bananas yellow 
>