首页 > 解决方案 > 尝试添加换行符以使其正常工作,但无法使其正常工作

问题描述

我是 R 的新手,所以请耐心等待!试图为大学班级做一个项目。

这是我的功能:

function(x) {
    y = seq(from = 1, to = x, by = 1)
    f = cat(paste("This is Banana", y, sep="\n"))
    return(f)
}

从这里的一些搜索中,我学会了添加catsep="\n"插入换行符。但是它将它们插入错误的位置:

当我运行函数(例如x=3)时,它会执行此操作并将数字跳到下一行

This is Banana
1 This is Banana
2 This is Banana
3 NULL

我希望它看起来像这样:

This is Banana 1
This is Banana 2
This is Banana 3

任何建议表示赞赏,非常感谢。

标签: r

解决方案


摆脱f并移动结束括号paste(您也可以简化seq):

myfun <- function(x) {
    y <- seq(x)
    cat(paste("This is Banana", y), sep="\n")
}

myfun(3)
This is banana 1 
This is banana 2 
This is banana 3 

推荐阅读