2017-10-11 12 views
2

のテキストと最も高いランクの単語にマッチする、私は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"列に含まれる単語とを一致させ、df2の最高位の一致単語を含むdf1に新しい列を挿入します。したがって、このような何か:

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

ここにはがあります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 
> 
関連する問題