r - 尝试添加换行符以使其正常工作,但无法使其正常工作
问题描述
我是 R 的新手,所以请耐心等待!试图为大学班级做一个项目。
这是我的功能:
function(x) {
y = seq(from = 1, to = x, by = 1)
f = cat(paste("This is Banana", y, sep="\n"))
return(f)
}
从这里的一些搜索中,我学会了添加cat
和sep="\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
任何建议表示赞赏,非常感谢。
解决方案
摆脱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
推荐阅读
- python-3.x - 在 tkinter 中将文件类型控制为 pdf
- c# - 在 PostAsync 中传递整数参数
- python-3.x - 如何将日期时间添加为 dict 值?
- go - 从 Golang 调用 Windows API
- html - 仅图像上的阴影,而不是整个宽度?
- php - Codeigniter - 登录在 if 部分不起作用
- c# - ArgumentException:BayesServer.Layout 引用了程序集 Microsoft.Msagl
- intellij-idea - 将 Kotlin 配置到 IntelliJ 中的现有项目
- asp.net-core - 如何创建 .Net Core MVC 库
- javascript - 我在使用 fancybox 插件时遇到问题