如何在R中打印文本和计算值

时间:2013-09-13 20:24:30

标签: r

在以下代码的末尾,我想打印n_species,但我首先要打印"物种的数量是"然后是价值。我怎么能这样做?

n_species <- 0
n_invisibility <- 0
for(i in Species) {
    n_species <- n_species + 1

    for(i in Invisibility){
        if(i == "Y") {
            n_invisibility <- n_invisibility + 1
        }
        else {
            n_invisibility <- n_invisibility + 0
        }
    }
}
print(n_species)
print(n_invisibility)

2 个答案:

答案 0 :(得分:2)

另一个允许轻松控制数字格式的选项:

sprintf("The number of species is %i.", n_species)

答案 1 :(得分:1)

print(paste("The number of species is",n_species))

粘贴也会获取参数sep,其中默认值为sep=" ",并且如果您尝试将其作为字符串打印(以及更多),则折叠基本上是矢量分离。