1

我有两个数据框

word_table <- word_9 word_1 word_3 ...word_random word_2 na na ...word_random word_5 word_3 na ...word_random

dictionary_words <- word_2 word_3 word_4 word_6 word_7 word_8 word_9 . . . word_n 我在寻找什么,将word_table与匹配dictionary_words并将单词替换为字典中可用的单词位置,就像这样,

result <- 7 na 2 ... 1 na na ... na 2 na ...

我已经尝试过pmatch, charmatch,match函数,result当它们的长度较小时返回正确的方式dictionary_words,但是当它相对较长(如超过 20000 个单词)时,result它只出现在第一列,其余的列就变成na这样了。

result <- 7 na na ... 1 na na ... na na na ...

有没有其他方法可以进行字符匹配,比如使用任何应用函数

样本

word_table <- data.frame(word_1 <- c("conflict","", "resolved", "", "", ""), word_2 <- c("", "one", "tricky", "one", "", "one"), 
                 word_3 <- c("thanks","", "", "comments", "par",""),word_4 <- c("thanks","", "", "comments", "par",""), word_5 <- c("", "one", "tricky", "one", "", "one"), stringsAsFactors = FALSE)
colnames(word_table) <- c("word_1", "word_2", "word_3", "word_4", "word_5")
## Targeted Words
dictionary_words <- data.frame(cbind(c("abovementioned","abundant","conflict", "thanks", "tricky", "one", "two", "three","four", "resolved")))

## convert into matrix (if needed)
word_table <- as.matrix(word_table)
dictionary_words <- as.matrix(dictionary_words)

## pmatch for each of the element in the dataframe (dt)
# matched_table <- pmatch(dt, TargetWord)
# dim(matched_table) <- dim(dt)
# print(matched_table) 

result <- `dim<-`(pmatch(word_table, dictionary_words, duplicates.ok=TRUE), dim(word_table))
print(result) # working fine, but when the dictionary_words is large, returning result for only first column of the word_table
4

1 回答 1

0

这是一个可重现的示例:

 word_table <- structure(list(V1 = structure(c(3L, 1L, 2L), .Label = c("word_2", 
                                                    "word_5", "word_9"), class = "factor"), V2 = structure(c(1L, 
                                                                                                             NA, 2L), .Label = c("word_1", "word_3"), class = "factor"), V3 = structure(c(1L, 
                                                                                                                                                                                          NA, NA), .Label = "word_3", class = "factor"), V4 = structure(c(1L, 
                                                                                                                                                                                                                                                          1L, 1L), .Label = "...word_random", class = "factor")), .Names = c("V1", 
                                                                                                                                                                                                                                                                                                                             "V2", "V3", "V4"), class = "data.frame", row.names = c(NA, -3L
                                                                                                                                                                                                                                                                                                                             ))

 dictionary_words <- structure(list(V1 = structure(1:7, .Label = c("word_2", "word_3", 
                                                              "word_4", "word_6", "word_7", "word_8", "word_9"), class = "factor")), .Names = "V1", class = "data.frame", row.names = c(NA, 
                                                                                                                                                                                        -7L))

您可以使用sapply

> sapply(word_table, function(x) match(x, dictionary_words[, 1]))
     V1 V2 V3 V4
[1,]  7 NA  2 NA
[2,]  1 NA NA NA
[3,] NA  2 NA NA

或者apply,如果您愿意:

> apply(word_table, 2, function(x) match(x, dictionary_words[, 1]))
V1 V2 V3 V4
[1,]  7 NA  2 NA
[2,]  1 NA NA NA
[3,] NA  2 NA NA
于 2016-04-16T04:05:20.903 回答