鉴于
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))
我想从分位数创建标签矢量。目前,我这样做
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真的很难看。我能以更优雅的方式做到这一点吗?
答案 0 :(得分:10)
head
和tail
会为您提供需要粘贴的分位数片段。
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"