R语言 将字符向量/字符列表按字符串长度排序
核心思路:将字符向量或者字符列表转化为数据帧,利用dplyr包的arrange函数排序。示例如下:#1.初始化测试数据
attr<-"This procedure read the BallMapper object from file. The parameter of the file is filename. We assume that files: filename_vertices filename_edges filename_edges_strength filename_points_covered_by_landmarks filename_landmarks filename_coloring"
#2.字符串转换为字符列表
a<-strsplit(attr,"[ .,;:{}()*]") #将英文句子拆分成单词,多个分隔符的时候需要用中括号括起来
#3.得到字符向量
b1<-unlist(a)
#4.字符向量转化为数据帧
b2<-data.frame(x=b1) #先将上述得到的列表转成数据帧,列名设置为x
#5.排序
b3<-arrange(b2,-nchar(b2$x)) #利用dplyr包的arrange函数排序,-nchar(b1$x)即按字符串长度排倒序
#6.结果转换为字符向量
b<-b2[,1] #从排序后的数据帧中取出想要的内容,转换为字符向量
页:
[1]