以下是我正在嘗試執行的操作: 當我正在分析的術語是「蘋果」時,我想知道需要多少換位符到「蘋果」,以便它可以在字符串中找到。計算字符串所需的換位符以便可以在另一個字符串中找到
「現在購買蘋果」=> 0需要換位(蘋果存在)。
「cheap aples online」=>需要1個換位(蘋果換成aples)。
「在這裏找到你需要的東西」=>需要2個換位(蘋果到蘋果)。
「aple」=> 2轉座需要(蘋果到aple)。
「bananas」=>需要5個換位(蘋果去香蕉)。
stringdist和adist函數不起作用,因爲它們告訴我需要多少換位才能將一個字符串轉換爲另一個字符串。總之,這裏是我寫到目前爲止:
#build matrix
a <- c(rep("apples",5),rep("bananas",3))
b <- c("buy apples now","cheap aples online","find your ap ple here","aple","bananas","cherry and bananas","pumpkin","banana split")
d<- data.frame(a,b)
colnames(d)<-c("term","string")
#count transpositions needed
d$transpositions <- mapply(adist,d$term,d$string)
print(d)
好的,謝謝,我是否應該將它添加到標題中,或標籤是否足夠? – 2015-04-03 17:34:48
我編輯你的代碼(在我的答案)蘋果在'a < - c(代表(「蘋果」,5),代表(「香蕉」,3))' – infominer 2015-04-03 18:13:08
ouch,感謝infominer,讓我糾正它這個問題呢! – 2015-04-03 21:02:32