我正在努力掌握文本挖掘并确定单词频率。我刚刚开始了解R及其软件包,我只是发现了tm(经过一段时间的阅读,我觉得这可能解决了我的问题)。
我的问题是:如何确定整个列中字符串中最常用的两个?
我有以下示例:
structure(list(Location = c("Chicago", "Chicago", "Chicago",
"LA", "LA", "LA", "LA", "LA", "LA", "Texas", "Texas", "Texas",
"Texas", "Texas"), Code = c(4450L, 4450L, 4450L, 4450L, 4450L,
4450L, 4450L, 4450L, 4450L, 4410L, 4410L, 4410L, 4410L, 4410L
), Description = c("LABOR - CROSSOVER BOARD BRACKET", "LABOR - CROWN DOOR GASKET",
"LABOR - CROWN DOOR GASKET - APPLY 4' NEW GASKET AND RE-APPLY",
"LABOR - CUSHIONING DEVICE - END OF CAR CUSTOMER SUPPLIED MATERIAL",
"LABOR - DOOR EDGE", "LABOR - DOOR GASKET, CROWN CORNER", "LABOR - DOOR LOCK POCKET STG",
"LABOR - DOOR LOCK RECEPTICALS STG", "LABOR - DOOR LOCK STG",
"BOLT, HT, UNDER 5/8\"\" DIA & 6\"\" - SIDE POST", "BOLT, HT, UNDER 5/8\"\" DIA & 6\"\" - TRAINLINE TROLLEY",
"BOLT,HT,5/8 IN.DIA.OR LESS UNDER 6\"\" LONG - BRAKE STEP", "BOLT,HT,5/8 IN.DIA.OR LESS UNDER 6\"\" LONG - CROSSOVER BOARD",
"BOLT,HT,5/8 IN.DIA.OR LESS UNDER 6\"\" LONG - CROSSOVER BOARD BRACKET"
), `Desired Description Based on frequency` = c("Labor - CROWN DOOR GASKET",
"Labor - CROWN DOOR GASKET", "Labor - CROWN DOOR GASKET", "Labor - DOOR LOCK",
"Labor - DOOR LOCK", "Labor - DOOR LOCK", "Labor - DOOR LOCK",
"Labor - DOOR LOCK", "Labor - DOOR LOCK", "Bolt - HT", "Bolt - HT",
"Bolt - HT", "Bolt - HT", "Bolt - HT")), .Names = c("Location",
"Code", "Description", "Desired Description Based on frequency"
), row.names = c(NA, -14L), class = "data.frame")
最后我希望我能添加这一栏:
Desired Description Based on frequency
Labor - CROWN DOOR GASKET
Labor - CROWN DOOR GASKET
Labor - CROWN DOOR GASKET
Labor - DOOR LOCK
Labor - DOOR LOCK
Labor - DOOR LOCK
Labor - DOOR LOCK
Labor - DOOR LOCK
Labor - DOOR LOCK
Bolt - HT
Bolt - HT
Bolt - HT
Bolt - HT
Bolt - HT
基本上我想评估所有4450或4410并查看表中的所有描述,这是最常见的并将其添加为列。另一个条件是基于位置。有人可以帮我一个简单的例子吗?
非常感谢
答案 0 :(得分:1)
我认为这不是一个适合您问题的解决方案。 (从以下事实开始,对于描述中采用哪个或多少个单词没有确切的规则。)然而,这里有两种快速和肮脏的方法,这可能有助于作为起点:
library(tm)
txts <- gsub("[^A-Z]", " ", df$Description)
groups <- paste(df$Location, df$Code)
# 1
opts <- list(tolower=F, removePunctuation=TRUE, wordLengths=c(2, Inf))
lst <- split(txts, groups)
res <- sapply(lst, function(x) {
freq <- termFreq(paste(x, collapse=" "), opts)/length(x)
paste(names(freq[rank(-freq, ties.method = "first")<=3]), collapse = " - ")
})
rep(res, lengths(lst))
# 2
lst <- lapply(strsplit(txts, "\\s+"), function(x) x[1:min(c(3,length(x)))] )
lst <- split(lst, groups)
n <- lengths(lst)
lst <- mapply("/", lapply(lst, function(x) sort(table(unlist(x)), decreasing = T)), n)
rep(sapply(lst, function(x) paste(names(x)[x>=.5], collapse=" - ")), n)