Есть ли способ точно сопоставить строку в одном столбце с парой строк в другом столбце в R? - PullRequest
1 голос
/ 06 апреля 2019

Я бы хотел сопоставить строку в одном столбце в R со строками, разделенными запятой "," в другом столбце

У меня было два кадра данных в R:

General_df
Main_cat   gen_cat
Fruits     apple
Fruits     mango
Fruits     strawberry
Vegetable  potato
Vegetable  lettuce
Vegetable  onion
Liquids    water
Liquids    milk
Liquids    juice
Tech       app
Object     straw


My_dataframe

Days      cat
Day 1     apple, potato, milk
Day 2     onion, water
Day 3     strawberry, potato
Day 4     straw, mango

Я хотел получить Main_cat для "My_dataframe", поэтому мне удалось получить это:

Days      cat                    Match_string Main_cat

Day 1     apple, potato, milk    apple        Fruits
Day 1     apple, potato, milk    potato       Vegetable
Day 1     apple, potato, milk    app          Tech
Day 1     apple, potato, milk    milk         Liquids

Он также соответствует подстроке "app", и у меня есть несколько таких подстрок для нескольких строк в моем фрейме данных

однако, я просто хочу, чтобы он точно совпадал со всей строкой в ​​столбце "cat", которая разделена символом ","

Days      cat                    Match_string Main_cat
Day 1     apple, potato, milk    apple        Fruits
Day 1     apple, potato, milk    potato       Vegetable
Day 1     apple, potato, milk    milk         Liquids

Есть ли способ найти точно совпадающую строку в этом сценарии? Спасибо!

General_df <- read.table(text='
Main_cat   gen_cat
Fruits     apple
Fruits     mango
Fruits     strawberry
Vegetable  potato
Vegetable  lettuce
Vegetable  onion
Liquids    water
Liquids    milk
Liquids    juice
Tech       app
Object     straw', header=TRUE, stringsAsFactors = FALSE)


My_dataframe <- read.table(text='
Days;    cat
Day 1;    apple, potato, milk
Day 2;    onion, water
Day 3;    strawberry, potato
Day 4 ;   straw, mango', sep=';', header=TRUE, stringsAsFactors = FALSE)

My_dataframe[] <- lapply(My_dataframe, trimws)

1 Ответ

2 голосов
/ 06 апреля 2019

Я думаю, это то, что вы ищете:

library(dplyr); library(tidyr)

My_dataframe %>%
    ## Split cat variable up into individual strings as a list column
    mutate(Match_string = strsplit(cat, ',\\s+')) %>%
    ## unnest the list into a long/tall data frame
    unnest(Match_string) %>%
    ## Join the lookup/key onto the tall/long data on the split column
    left_join(General_df, by = c('Match_string' = 'gen_cat'))


##   Days  cat                 Match_string Main_cat 
##   <chr> <chr>               <chr>        <chr>    
## 1 Day 1 apple, potato, milk apple        Fruits   
## 2 Day 1 apple, potato, milk potato       Vegetable
## 3 Day 1 apple, potato, milk milk         Liquids  
## 4 Day 2 onion, water        onion        Vegetable
## 5 Day 2 onion, water        water        Liquids  
## 6 Day 3 strawberry, potato  strawberry   Fruits   
## 7 Day 3 strawberry, potato  potato       Vegetable
## 8 Day 4 straw, mango        straw        Object   
## 9 Day 4 straw, mango        mango        Fruits   

И базовый подход R, чтобы убедиться, что я не слишком зависим:

Match_string <- strsplit(My_dataframe$cat, ',\\s+')
data.frame(
    My_dataframe[rep(seq_len(nrow(My_dataframe)), lengths(Match_string)),],
    Match_string = unlist(Match_string), 
    Main_cat = General_df$Main_cat[match(unlist(Match_string), General_df$gen_cat)],
    stringsAsFactors = FALSE,
    row.names = NULL
)

##    Days                 cat Match_string  Main_cat
## 1 Day 1 apple, potato, milk        apple    Fruits
## 2 Day 1 apple, potato, milk       potato Vegetable
## 3 Day 1 apple, potato, milk         milk   Liquids
## 4 Day 2        onion, water        onion Vegetable
## 5 Day 2        onion, water        water   Liquids
## 6 Day 3  strawberry, potato   strawberry    Fruits
## 7 Day 3  strawberry, potato       potato Vegetable
## 8 Day 4        straw, mango        straw    Object
## 9 Day 4        straw, mango        mango    Fruits

Или data.table , если вам нужна скорость и память:

library(data.table)
merge(
    data.table(My_dataframe)[, Match_string := strsplit(cat, ',\\s+')][, 
        .(Match_string =unlist(Match_string)), by = c('Days', 'cat')], 
    General_df, by.x = 'Match_string', by.y = 'gen_cat',
    all.x = TRUE
)[order(Days), .(Days, cat, Match_string, Main_cat)]

##     Days                 cat Match_string  Main_cat
## 1: Day 1 apple, potato, milk        apple    Fruits
## 2: Day 1 apple, potato, milk         milk   Liquids
## 3: Day 1 apple, potato, milk       potato Vegetable
## 4: Day 2        onion, water        onion Vegetable
## 5: Day 2        onion, water        water   Liquids
## 6: Day 3  strawberry, potato       potato Vegetable
## 7: Day 3  strawberry, potato   strawberry    Fruits
## 8: Day 4        straw, mango        mango    Fruits
## 9: Day 4        straw, mango        straw    Object
...