基本上,我有以下说法:
counter <- 3
k <- 9999
Run Code Online (Sandbox Code Playgroud)
我想让R打印以下内容:
on the 3rd count: 9999
Run Code Online (Sandbox Code Playgroud)
我应该使用什么命令来执行此操作?请把它拼出来,因为我对R来说是全新的.
Dre*_*een 16
基本结构是
paste("on the ", counter, "rd count: ", k, sep="")
Run Code Online (Sandbox Code Playgroud)
你必须有点聪明才能为数字选择正确的后缀(即3之后的"rd",4-9之后的"th"等等.这是一个功能:
suffixSelector <- function(x) {
if (x%%10==1) {
suffixSelector <- "st"
} else if(x%%10==2) {
suffixSelector <- "nd"
} else if(x%%10==3) {
suffixSelector <- "rd"
} else {
suffixSelector <- "th"
}
Run Code Online (Sandbox Code Playgroud)
}
从而:
suffix <- suffixSelector(counter)
paste("on the ", counter, suffix, " count: ", k, sep="")
Run Code Online (Sandbox Code Playgroud)
您需要设置sep
参数,因为默认情况下会paste
在字符串之间插入空格.