R -- 体验 stringdist

文章目录

  • 安装
  • 使用
    • [stringdist :返回列表](#stringdist :返回列表)
    • [stringdistmatrix :返回矩阵](#stringdistmatrix :返回矩阵)
  • [amatch & ain](#amatch & ain)
  • 延伸:距离计算公式
      • [Hamming distance](#Hamming distance)
      • [Longest Common Substring distance](#Longest Common Substring distance)
      • [Levenshtein distance (weighted)](#Levenshtein distance (weighted))
      • [The optimal string alignment distance dosa](#The optimal string alignment distance dosa)
      • [Full Damerau-Levenshtein distance (weighted)](#Full Damerau-Levenshtein distance (weighted))
      • [Q-gram distance](#Q-gram distance)
      • [Jaccard distance for q-gram count vectors (= 1-Jaccard similarity)](#Jaccard distance for q-gram count vectors (= 1-Jaccard similarity))
      • [cosine distance for q-gram count vectors (= 1-cosine similarity)](#cosine distance for q-gram count vectors (= 1-cosine similarity))
    • [At last](#At last)

安装

R 复制代码
install.packages('stringdist')

or

bash 复制代码
git clone https://github.com/markvanderloo/stringdist.git
cd stringdist
bash ./build.bash
R CMD INSTALL output/stringdist_*.tar.gz

使用

The package offers the following main functions:

  • stringdist computes pairwise distances between two input character vectors (shorter one is recycled)
  • stringdistmatrix computes the distance matrix for one or two vectors
  • stringsim computes a string similarity between 0 and 1, based on stringdist
  • amatch is a fuzzy matching equivalent of R's native match function
  • ain is a fuzzy matching equivalent of R's native %in% operator
  • afind finds the location of fuzzy matches of a short string in a long string.
  • seq_dist, seq_distmatrix, seq_amatch and seq_ain for distances between, and matching of integer sequences.

stringdist :返回列表

stringdist(
  a,
  b,
  method = c("osa", "lv", "dl", "hamming", "lcs", "qgram", "cosine", "jaccard", "jw",
    "soundex"),
  useBytes = FALSE,
  weight = c(d = 1, i = 1, s = 1, t = 1),
  q = 1,
  p = 0,
  bt = 0,
  nthread = getOption("sd_num_thread")
)

a	:R object (target); will be converted by as.characte
b	 :R object (source); will be converted by as.character This argument is optional for stringdistmatrix (see section Value).
method	 :Method for distance calculation. 
useBytes	:Perform byte-wise comparison
weight	:For method='osa' or 'dl', the penalty for deletion, insertion, substitution and transposition, in that order. 
	 When method='lv', the penalty for transposition is ignored.
	 When method='jw', the weights associated with characters of a, characters from b and the transposition weight, in that order. 
	 Weights must be positive and not exceed 1. 
	 weight is ignored completely when method='hamming', 'qgram', 'cosine', 'Jaccard', 'lcs', or soundex.

q	:Size of the q-gram; must be nonnegative. Only applies to method='qgram', 'jaccard' or 'cosine'.
p	:Prefix factor for Jaro-Winkler distance. The valid range for p is 0 <= p <= 0.25.
	 If p=0 (default), the Jaro-distance is returned. Applies only to method='jw'.
bt	:Winkler's boost threshold. Winkler's prefix factor is only applied when the Jaro distance is larger than bt. Applies only to method='jw' and p>0.
useNames	:Use input vectors as row and column names?

example

注意:String distance functions have two possible special output values.

NA is returned whenever at least one of the input strings to compare is NA .

And Inf is returned when the distance between two strings is undefined according to the selected algorithm.

R 复制代码
stringdist("bar","foo",method = "lv") #使用的是Levenshtein distance  & return  3
stringdist("ba","foo",method = "lv") #使用的是Levenshtein distance  &  return  3 ,注意这里是不等长的序列

stringdist('fu', 'foo', method='hamming') # 使用的是 Hamming distance &  return Inf

stringdistmatrix :返回矩阵

stringdistmatrix(
  a,
  b,
  method = c("osa", "lv", "dl", "hamming", "lcs", "qgram", "cosine", "jaccard", "jw",
    "soundex"),
  useBytes = FALSE,
  weight = c(d = 1, i = 1, s = 1, t = 1),
  q = 1,
  p = 0,
  bt = 0,
  useNames = c("none", "strings", "names"),
  nthread = getOption("sd_num_thread")
)
Arg

example

- 只输入一个vertor:返回一个 dist函数的结果
- 输入两个vector :返回矩阵

amatch & ain

  • Function amatch(x,table) finds the closest match of elements of x in table. When multiple equivalent matches are found, the
    first match is returned
  • A call to ain(x,table) returns a logical vector indicating which elements of x were (approximately) matched in table.
  • Both amatch and ain have been designed to approach the behaviour of R's native match and %in% functionality as much as possible. By default amatch and ain locate exact matches, just like match.
  • This may be changed by increasing the maximum string distance between the search pattern and elements of the lookup table.

amatch仿照R base function match进行设计,通过 参数maxDist控制该函数的行为,如果maxDist 设置的很小其表现近似于 exact match,当 maxDist 设置的比较大时则表现的是approximately match。amtch 与 ain的区别类似于match和 %in%,一个返回元素的index,一个返回TRUE/FALSE。

R 复制代码
amatch('fu', c('foo','bar')) # return NA
amatch('fu', c('foo','bar'), maxDist=2) # return 1

ain('fu', c('foo','bar')) # return FALSE
ain('fu', c('foo','bar'), maxDist=2) # return  TRUE
ain('bar', c('foo','bar')) # return TRUE
ain('bar', c('foo','bar'), maxDist=2) # return TRUE

延伸:距离计算公式

Hamming distance


Longest Common Substring distance



Levenshtein distance (weighted)


The optimal string alignment distance dosa

Full Damerau-Levenshtein distance (weighted)



注意,D~osa~ 和D~dl~的区别主要是最后一个方程式,D~osa~只允许前后相邻的两个字符串置换,D~dl~则允许当前的字符串和其他的字符置换后计算距离



Q-gram distance

Jaccard distance for q-gram count vectors (= 1-Jaccard similarity)

cosine distance for q-gram count vectors (= 1-cosine similarity)

  • Jaro distance

At last

相关推荐
一声沧海笑17 小时前
dplyr、tidyverse和ggplot2初探
信息可视化·数据分析·r语言
waterHBO21 小时前
R语言 基础笔记
开发语言·笔记·r语言
Red Red1 天前
GEO数据库提取疾病样本和正常样本|GEO数据库区分疾病和正常样本|直接用|生物信息|生信
开发语言·数据库·笔记·学习·r语言·c#·生物信息
邢博士谈科教3 天前
比传统机器学习更先进的深度学习神经网络的二分类建模全流程教程
数据挖掘·r语言·数据可视化
环能jvav大师5 天前
基于R语言的统计分析基础:使用键盘输入数据
开发语言·学习·数据分析·r语言·人机交互
Red Red5 天前
GEO数据的下载和处理|GEO数据转换为Gene symbol|GEO注释文件提取symbol|查看样本标签|查看GEO数据疾病或正常|生物信息基础
数据库·笔记·学习·r语言·生物信息·geo数据库
不是伍壹5 天前
【R语言】删除数据框中所有行中没有大于200的数值的行
开发语言·r语言
hongyanwin5 天前
商业预测 初识R
r语言·预测
maizeman1265 天前
R语言统计分析——用回归做ANOVA
回归·r语言·方差分析·anova·线性模型
武艺晴小朋友你好6 天前
基于scRNA-data,运用pySCENIC寻找细胞群里面活跃的调节子
r语言·数据可视化·r语言-4.2.1