在连接函数时我必须使用什么命令来添加换行符?
这是脚本:
canada <- c(50, 50, 50)
korea <- c(70, 70, 70)
brazil <- c(100, 100, 100)
fruit <- rbind(canada, korea, brazil)
colnames(fruit) <- c("apple", "orange", "banana")
one <- function(x){
x <- tolower(x) # assuming all row names are in lower case
myrow <- fruit[x,]
count <- sapply(seq_along(myrow),
function(x, n, i)
{paste0(x[i], "")},
x=myrow[1], n=names(myrow))
count[length(count)] <- paste0(count[length(count)])
count <- paste(count[1])
cat(tools::toTitleCase(x), "has", count, "thousand farms") # General statement
}
以下是我尝试的内容:
cat(one("canada"), '\n\n', one("canada"))
Canada has 50 thousand farmsCanada has 50 thousand farms
我希望它看起来像这样:
Canada has 50 thousand farms
Canada has 50 thousand farms
答案 0 :(得分:1)
你的问题是,在你的功能中,你使用cat,你应该使用粘贴(检查?粘贴和?cat来理解差异)。有了这个功能,它应该可以正常工作:
one <- function(x){
x <- tolower(x) # assuming all row names are in lower case
myrow <- fruit[x,]
count <- sapply(seq_along(myrow),
function(x, n, i)
{paste0(x[i], "")},
x=myrow[1], n=names(myrow))
count[length(count)] <- paste0(count[length(count)])
count <- paste(count[1])
ret <- paste(tools::toTitleCase(x), "has", count, "thousand farms") # General statement
}
为了从第二行删除尾随空格,你必须在最后一个语句中添加sep =“”:
cat(one("canada"), ' \n\n ', one("canada"), sep="")