国会图书馆分类号在图书馆中用于为事物提供电话号码,以便在书架上订购。它们可以是简单的,也可以是相当复杂的,只有一些强制性部分,但许多是可选的。 (参见 050 Library of Congress Call Number 上的“在 050 中输入索书号”了解它们如何分解,或lc_callnumber 了解对它们进行排序的 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"
),因为我不知道它应该是什么。
最佳答案
这可能是一种更简单的方法。这假设每个数字都具有以下格式:2 个字母的代码、空格、数字、空格、字母数字、空格...年份。
策略是将 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)]
对于这个有限的数据集,该技术有效。
关于r - 如何按 R 中的国会图书馆分类 (LCC) 编号排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45240337/