R中的顺序引用编号:如果是连续的,则用连字符号分开

编程入门 行业动态 更新时间:2024-10-24 02:38:05
本文介绍了R中的顺序引用编号:如果是连续的,则用连字符号分开-如果不是,则添加逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想为R中的数字生成顺序引用数字.数字应如果它们是连续的,则用连字符分隔.否则,数字之间用逗号分隔.例如,数字1, 2, 3, 5, 6, 8, 9, 10, 11 and 13应显示为1-3,5,6,8-11,13.

I want to generate sequential citation numbers for a figure in R. The numbers should be separated by a hyphen, if they are sequential. Otherwise the numbers are separated by a comma. For example, numbers 1, 2, 3, 5, 6, 8, 9, 10, 11 and 13 should come out as 1-3,5,6,8-11,13.

此问题已先前针对c#的答案,我已经编写了一个适用于R的函数,但是可以对该函数进行改进.我将这个问题发布给其他可能有类似需求的人作为参考.如果您发现有关R的类似问题(我没有找到),请投票关闭,我将删除该问题.

This question has been previously answered for c#, and I have written a function that works for R, but this function can be improved. I post this question as a reference for others that might have a similar need. If you find a similar question for R (which I did not), please vote to close and I will remove the question.

下面的功能不是很优雅,但是似乎可以完成工作. 如何使功能更短,更美观?

The function below is not very elegant, but seems to do the job. How to make the function shorter and more elegant?

x <- c(1,2,3,5,6,8,9,10,11,13) library(zoo) ## the function requires zoo::na.approx function ##' @title Generate hyphenated sequential citation from an integer vector ##' @param x integer vector giving citation or page numbers ##' @importFrom zoo na.approx seq.citation <- function(x) { ## Result if lenght of the integer vector is 1. if(length(x) == 1) return(x) else { ## Sort x <- sort(x) ## Difference df <- diff(x) ## Index to determine start and end points ind <- c("start", rep("no", length(df)-1), "end") ind[which(df > 1)] <- "end" ## Temporary start point vector sts <- which(ind == "end") + 1 ind[sts[sts < length(ind)]] <- "start" ## Replace the first index element ind[1] <- "start" ## Replace the last index element, if preceding one is "end" if(ind[length(ind)-1] == "end") ind[length(ind)] <- "start" ## Groups for comma separation using "start" as the determining value. grp <- rep(NA, length(x)) grp[which(ind == "start")] <- 1:length(grp[which(ind == "start")]) grp <- zoo::na.approx(grp, method = "constant", rule = 2) ## Split sequences by group seqs <- split(x, grp) seqs <- lapply(seqs, function(k) { if(length(k) == 1) k else { if(length(k) == 2) paste(k[1], k[2], sep = ",") else { paste(k[1], k[length(k)], sep = "-") }} }) ## Result return(do.call("paste", c(seqs, sep = ","))) } } seq.citation(x) # [1] "1-3,5,6,8-11,13"

推荐答案

您可以使用tapply,

paste(tapply(x, cumsum(c(1, diff(x) != 1)), function(i) ifelse(length(i) > 2, paste0(head(i, 1), '-', tail(i, 1)), paste(i, collapse = ','))), collapse = ',') [1] "1-3,5,6,8-11,13"

更多推荐

R中的顺序引用编号:如果是连续的,则用连字符号分开

本文发布于:2023-11-29 13:40:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1646538.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:则用   顺序   字符   编号

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!