国会图书馆分类号在图书馆中被用来给东西提供索引号,这样它们就可以在书架上排序。它们可以是简单的,也可以是相当复杂的,有几个强制性的部分,但有许多可选的部分。(请参阅050 Library of Congress Call Number上的“在050中输入借阅号”了解它们的分类方式,或参阅lc了解对它们进行排序的Ruby工具。)
我想在R中按LCC编号排序,我看过Sort a list of nontrivial elements in R和Sorting list of list of elements of a custom class in R?,但还没有弄清楚。
这里有四个电话号码,按顺序输入:
call_numbers <- c("QA 7 H3 1992", "QA 76.73 R3 W53 2015", "QA 90 H33 2016", "QA 276.45 R3 A35 2010")sort按字符对它们进行排序,因此276 <7< 76.73 < 90。
> sort(call_numbers)
[1] "QA 276.45 R3 A35 2010" "QA 7 H3 1992" "QA 76.73 R3 W53 2015" "QA 90 H33 2016" 为了正确地对它们进行排序,我认为我必须定义一个类,然后在它上面定义一些方法,如下所示:
library(stringr)
class(call_numbers) <- "LCC"
## Just pick out the letters and digits for now, leave the rest
## until sorting works, then work down more levels.
lcc_regex <- '([[:alpha:]]+?) ([[:digit:]\\.]+?) (.*)'
"<.LCC" <- function(x, y) {
x_lcc <- str_match(x, lcc_regex)
y_lcc <- str_match(y, lcc_regex)
if(x_lcc[2] < y_lcc[2]) return(x)
if(as.integer(x_lcc[3]) < as.integer(y_lcc[3])) return(x)
}
"==.LCC" <- function(x, y) {
x_lcc <- str_match(x, lcc_regex)
y_lcc <- str_match(y, lcc_regex)
x_lcc[2] == y_lcc[2] && x_lcc[3] == y_lcc[3]
}
">.LCC" <- function(x, y) {
x_lcc <- str_match(x, lcc_regex)
y_lcc <- str_match(y, lcc_regex)
if(x_lcc[2] > y_lcc[2]) return(x)
if(as.integer(x_lcc[3]) > as.integer(y_lcc[3])) return(x)
}这不会改变排序顺序。我没有定义一个子集方法("[.myclass"),因为我不知道它应该是什么。
发布于 2018-04-11 10:53:09
来自gtools包(标准R的一部分)的mixedsort被证明能做到这一点:
library(gtools)
call_numbers <- c("QA 7 H3 1992", "QA 76.73 R3 W53 2015", "QA 90 H33 2016", "QA 276.45 R3 A35 2010")
mixedsort(call_numbers)
## [1] "QA 7 H3 1992" "QA 76.73 R3 W53 2015" "QA 90 H33 2016" "QA 276.45 R3 A35 2010"此外,mixedorder可用于按一列对数据帧进行排序。
这是前面在How to sort a character vector where elements contain letters and numbers in R?中回答的一种特殊情况
发布于 2017-07-22 06:37:25
这可能是一种更简单的方法。这里假设每个数字都有以下格式:两个字母的代码,空格,数字,空格,字母数字,space...Year。
该策略是将LOC号用空格拆分,然后获得前3个字段的3列数据,然后使用order函数对每列进行顺序排序。
call_numbers <- c("QA 7 H3 1992", "QA 76.73 R3 W53 2015", "QA 90 H33 2016", "QA 276.45 R3 A35 2010")
#split on the spaces
split<-strsplit(call_numbers, " " )
#Retrieve the 2 letter code
letters<-sapply(split, function(x){x[1]})
#retrieve the 2nd number group and convert to numeric values for sorting
second<-sapply(split, function(x){as.numeric(x[2])})
#obtain the 3rd grouping
third<-sapply(split, function(x){x[3]})
#find the year
year<-sapply(split, function(x){x[length(x)]})
df<-data.frame(call_numbers)
#sort data based on the first and 2nd column
call_numbers[order(letters, second, third)]对于这个有限的数据集,该技术是有效的。
发布于 2017-07-22 00:01:05
我觉得我花了太多的时间来找出你想要做的事情的解决方案--只有我的解决方案是针对JavaScript的。但它基本上归结为这些数字的“标准化”概念,以便它们可以按字母顺序排序。
也许这个解决方案可以使用并移植到R。至少,希望这能让你开始。它包括一些正则表达式和一些额外的脚本,以使借阅号进入可以排序的状态。
https://github.com/rayvoelker/js-loc-callnumbers/blob/master/locCallClass.js
祝你好运!
https://stackoverflow.com/questions/45240337
复制相似问题