【问题标题】:finding similarity within data using grepl使用 grepl 查找数据中的相似性
【发布时间】:2019-06-09 07:23:34
【问题描述】:

我有一个像这样的大数据框:

df
id product
1  milk
2  200
3  gr.
4  Low
5  fat
6  milkshake
7  200
8  gr.
9  High
10 fat
...

对于每个单词,我需要了解哪些单词与之相似,我使用了 grepl,我可以单独对每个单词执行此操作,但我不知道如何将其应用于整个数据框。

matches1<-paste(grepl(words_unlist[1],words_unlist))
matches1<- as.data.frame(matches1)

id matches1
1  1
2  0
3  0
4  0
5  0
6  1
7  0
8  0
9  0
10 0

但我需要对所有单词都这样做。像这样:

df
id product     matches1   matches2   matches3   ... matches10
1  milk        1          0          0          ... 0
2  200         0          1          0          ... 0
3  gr.         0          0          1          ... 0
4  Low         0          0          0          ... 0
5  fat         0          0          0          ... 1
6  milkshake   1          0          0          ... 0
7  200         0          1          0          ... 0
8  gr.         0          0          1          ... 0
9  High        0          0          0          ... 0
10 fat         0          0          0          ... 1
...

【问题讨论】:

  • cheese 与第 6 行的 milk 有何相似之处?
  • 对不起tmfmnk,我编辑它:)

标签: r


【解决方案1】:

我们可以使用sapply 并将每个product 与整列df$Productgrepl 匹配

df[paste0("matches", seq_len(nrow(df)))] <- +(sapply(df$product, grepl, df$product))

df
#   id   product matches1 matches2 matches3 matches4 matches5 matches6 matches7 matches8 matches9 matches10
#1   1      milk        1        0        0        0        0        0        0        0        0         0
#2   2       200        0        1        0        0        0        0        1        0        0         0
#3   3       gr.        0        0        1        0        0        0        0        1        0         0
#4   4       Low        0        0        0        1        0        0        0        0        0         0
#5   5       fat        0        0        0        0        1        0        0        0        0         1
#6   6 milkshake        1        0        0        0        0        1        0        0        0         0
#7   7       200        0        1        0        0        0        0        1        0        0         0
#8   8       gr.        0        0        1        0        0        0        0        1        0         0
#9   9      High        0        0        0        0        0        0        0        0        1         0
#10 10       fat        0        0        0        0        1        0        0        0        0         1

【讨论】:

  • 看来milkmilkshake 不匹配。
  • @tmfmnk 我认为milk 匹配milkshake(col matches1 第6 行)但milkshake 不匹配milk 我认为OP 根据他们的尝试想要什么?
  • 你是对的,恰恰相反。很难说,我的印象是 OP 两者都想要。应该澄清一下。
【解决方案2】:

lapply 的选项

df[paste0("matches", seq_len(nrow(df)))] <- +(do.call(cbind, 
              lapply(df$product, grepl, df$product)))
df
#   id   product matches1 matches2 matches3 matches4 matches5 matches6 matches7 matches8 matches9 matches10
#1   1      milk        1        0        0        0        0        0        0        0        0         0
#2   2       200        0        1        0        0        0        0        1        0        0         0
#3   3       gr.        0        0        1        0        0        0        0        1        0         0
#4   4       Low        0        0        0        1        0        0        0        0        0         0
#5   5       fat        0        0        0        0        1        0        0        0        0         1
#6   6 milkshake        1        0        0        0        0        1        0        0        0         0
#7   7       200        0        1        0        0        0        0        1        0        0         0
#8   8       gr.        0        0        1        0        0        0        0        1        0         0
#9   9      High        0        0        0        0        0        0        0        0        1         0
#10 10       fat        0        0        0        0        1        0        0        0        0         1

或使用tidyverse

library(tidyverse)
df %>%
   mutate(similar = map(product, ~  
           str_detect(.x, df$product) %>% 
                     as.integer %>% 
                     as.list %>% 
                      set_names(str_c('matches', seq_len(nrow(df)))) %>% 
                     as_tibble )) %>%
    unnest

数据

df <- structure(list(id = 1:10, product = c("milk", "200", "gr.", "Low", 
  "fat", "milkshake", "200", "gr.", "High", "fat")), 
  class = "data.frame", row.names = c(NA, -10L))

【讨论】:

    猜你喜欢
    • 2013-02-23
    • 2016-09-01
    • 1970-01-01
    • 2010-12-03
    • 2011-04-19
    • 1970-01-01
    • 2018-07-13
    • 1970-01-01
    • 2016-03-14
    相关资源
    最近更新 更多