本文介绍了连接向量中的相邻字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给定
qz <- quantile(c(1,2,3,4,5,6,7,8,9,10), c(0.0, 0.2, 0.4, 0.6, 0.8, 1.0))
我想从分位数创建一个标签向量.目前,我这样做
I want to create a vector of labels from the quantiles. Currently, I do this
zlab <- c(paste(paste(sprintf(qz[[1]], fmt='$%.2f'), "-"), sprintf(qz[2],
fmt='$%.2f')), paste(paste(sprintf(qz[[2]], fmt='$%.2f'), "-"), sprintf(qz[3],
fmt='$%.2f')),paste(paste(sprintf(qz[[3]], fmt='$%.2f'), "-"), sprintf(qz[4],
fmt='$%.2f')), paste(paste(sprintf(qz[[4]], fmt='$%.2f'), "-"), sprintf(qz[5],
fmt='$%.2f')), paste(paste(sprintf(qz[[5]], fmt='$%.2f'), "-"), sprintf(qz[6],
fmt='$%.2f')))
并得到
zlab
[1] "$1.00 - $2.80" "$2.80 - $4.60" "$4.60 - $6.40" "$6.40 - $8.20" "$8.20 - $10.00"
zlab 的格式完全正确,最终会作为绘图上的标签.但是生成 zlab 真的很丑.我可以以更优雅的方式执行此操作吗?
zlab is formatted exactly correctly and eventually winds up as labels on a plot. But generating zlab is really ugly. Can I do this in a more elegant manner?
推荐答案
head
和 tail
为您提供需要粘贴的分位数切片.
head
and tail
give you the slices of the quantiles that you need to paste.
x <- sprintf("$%.2f", qz)
x
## [1] "$1.00" "$2.80" "$4.60" "$6.40" "$8.20" "$10.00"
paste(head(x, -1), tail(x, -1), sep=' - ')
## [1] "$1.00 - $2.80" "$2.80 - $4.60" "$4.60 - $6.40" "$6.40 - $8.20" "$8.20 - $10.00"
这篇关于连接向量中的相邻字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!