r - 如何在 R 中保存打印字符串中的数字
问题描述
我想保存使用 print() 函数 R 打印的数字。例如,我有一个 print(bf(X, Y) 命令打印句子“估计值为:0.5”。如何只保存数字0.5 到 txt/excel 文件?因为我有一个循环,可以打印数百个不同数字的句子,我希望能够自动将所有数字保存在文件中。谢谢!
解决方案
I agree with @Ben Bolker. Instead of operating on string you should return the number from the function bf
. You can take the print
statement outside the function.
For example, if the current state of the function is like this -
bf <- function(X, Y) {
#...some code
#...some code
#res <- calculation for res
print(c("Estimated value is : ", res))
}
Change it to -
bf <- function(X, Y) {
#...some code
#...some code
#res <- calculation for res
res
}
So you can save the output of the function in a variable (res <- bf(X, Y)
). If you need the print statement you can add it outside the function.
res <- bf(X, Y)
print(c("Estimated value is : ", res))
推荐阅读
- reactjs - 无法运行 gatsby develop - 加载本地开发命令时出现问题
- c# - IIS 返回 JSON 而不是文件(在导出到 Excel 功能中)
- javascript - 如何防止表单仅在捕获错误时才提交
- ios - 为什么我的静态库函数不返回整数?
- swift - 两个日期之间的差异没有快速显示秒数
- python-3.x - 在 np.array 中将 int 转换为 float
- php - 我可以在 PHP 中使用 DECLARE 作为 SQL 语句吗
- java - 当我为成绩簿应用程序输入成绩百分比时,我的 Java 编码出现错误
- thymeleaf - Thymeleaf 可以访问 Ktor 会话属性吗?
- powershell - 如何仅在空行上拆分