将聚类分析的结果转换为R

时间:2018-09-20 10:42:58

标签: r dataframe dplyr data.table

此帖子来自本主题 classifying identically pattern in words using R 解决方案很好,但是我需要数据帧格式。 数据是一样的

text<-structure(list(ID_C_REGCODES_CASH_VOUCHER = c(3941L, 3941L, 3941L, 
3945L, 3945L, 3945L, 3945L, 3945L, 3945L, 3945L, 3953L, 3953L, 
3953L, 3953L, 3953L, 3953L, 3960L, 3960L, 3960L, 3960L, 3960L, 
3960L, 3967L, 3967L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), GOODS_NAME = structure(c(19L, 
17L, 15L, 18L, 16L, 23L, 21L, 14L, 22L, 20L, 6L, 2L, 10L, 8L, 
7L, 13L, 5L, 11L, 7L, 12L, 4L, 3L, 9L, 9L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L
), .Label = c("", "* 2108609 SLOB.Mayon.OLIVK.67% 400ml", "* 3014084 D.Dym.Spikachki DEREVEN.MINI 1kg", 
"* 3398012 DD Kolb.SERV.OKHOTN in / to v / y0.35", "* 3426789 WH.The corn rav guava / yagn.d / CAT seed 85g", 
"197 Onion 1 kg", "2013077 MAKFA Makar.RAKERS 450g", "2030918 MARIA TRADITIONAL Biscuit 180g", 
"2049750 MAKFA Makar.SHIGHTS 450g", "3420159 LEBED.Mol.past.3,4-4,5% 900g", 
"3491144 LIP.NAP.ICE TEA green yellow 0.5 liter", "6788 MAKFA Makar.perya 450g", 
"809 Bananas 1kg", "FetaXa Cheese product 60% 400g (", "Lemons 55+", 
"MAKFA Macaroni feathers like. in / with", "Napkins paper color 100pcs PL", 
"Package \"Magnet\" white (Plastiktre)", "Pasta Makfa snail flow-pack 450 g.", 
"SHEBEKINSKIE Macaroni Butterfly №40", "SOFT Cotton sticks 100 PE (BELL", 
"TENDER AGE Cottage cheese 10", "TOBUS steering-wheel 0.5kg flow"
), class = "factor")), .Names = c("ID_C_REGCODES_CASH_VOUCHER", 
"GOODS_NAME"), class = "data.frame", row.names = c(NA, -61L))

让我们进行聚类分析

text <- text[1:24,]
library(quanteda)
library(tidyverse)
hc <- text %>% 
  pull(GOODS_NAME) %>% 
  as.character %>% 
  quanteda::tokens(
    remove_numbers = T,  
    remove_punct = T,
    remove_symbols = T, 
    remove_separators = T
  ) %>% 
  quanteda::tokens_tolower() %>% 
  quanteda::tokens_remove(valuetype="regex", pattern = c("^\\d.*")) %>% 
  quanteda::dfm() %>% 
  textstat_simil(method = "jaccard") %>% 
  magrittr::multiply_by(-1) %>% 
  `attr<-`("Labels", text$GOODS_NAME) %>% 
  hclust(method = "average") 


clusters <- cutree(hc, h = -0.1)
split(text, clusters)

代码完成后,如何将结果转换为data.frame?

预期产量

  

s = as.data.frame(拆分(文本,簇))   错误(函数(...,row.names = NULL,check.rows = FALSE,check.names = TRUE,:     参数暗示不同的行数:7、1、2

非常理想的输出

clust   ID_C_REGCODES_CASH_VOUCHER   GOODS_NAME
1       3941    Pasta Makfa snail flow-pack 450 g.
1       3945     MAKFA Macaroni feathers like. in / with
1       3953    2013077 MAKFA Makar.RAKERS 450g
1       3960    2013077 MAKFA Makar.RAKERS 450g
1       3960    6788 MAKFA Makar.perya 450g
1       3967    2049750 MAKFA Makar.SHIGHTS 450g
1       3967    2049750 MAKFA Makar.SHIGHTS 450g
2       3941    Napkins paper color 100pcs PL

1 个答案:

答案 0 :(得分:1)

library( data.table )
dt <- data.table::rbindlist( split(text, clusters) )[, clust := clusters]