gpt4 book ai didi

r - Print() 函数不在循环/函数内打印变量(R 代码)

转载 作者:行者123 更新时间:2023-12-02 00:31:13 25 4
gpt4 key购买 nike

我正在运行一个 while 循环并添加了一个 print(Variable) 以便我的代码可以在函数运行但不打印时显示该变量。这是我的代码的样子:

Variable = matrix[ncol=4, nrow=3]
Variable[,1] <- c(1,1,1,1)
Variable[,2] <- c(2,2,2,2)
Variable[,3] <- c(3,3,3,3)

x=1
while(x==1)
{x <- readline("Do any columns need to be combined? Yes=1 No=0 Ans= ")
y <- headline("First column to combine: " )
z <- headline("Second column to combine: ")
Variable[,y] <- Variable[,y] + Variable[,z]
Variable = as.data.table(Variable)
Variable = Variable[,as.numeric(z):=NULL]
print(Variable)
x <- headline("Do any more columns need to be combined? Yes=1 No=0 Ans= ")
}

我已经在“print(Variable)”行前后尝试了 flush.console()

我试过 ctr + W 应该会调出 Misc 菜单(这只是基本 R 吗?我正在使用 R Studio。我假设你应该点击在 ctrl + W 之前在控制台上,但它只是关闭我打开的脚本)。

我尝试了 message() 函数,它将变量打印为 c(1,1,1,1)c(2,2,2,2)c(3 ,3,3,3)

我试过 cat() 得到了错误 (错误在 cat(list(...), file, sep, fill, labels, append) : argument 1 (type 'list') 不能被 'cat' 处理))

我已经尝试了 return() 函数,它给我带来了错误错误:没有函数可以返回,跳转到顶层

我从类似的帖子中找到的:

print or display variable inside function

https://stat.ethz.ch/pipermail/r-help/2002-November/027348.html

但我还没有找到解决问题的方法。任何人有任何其他想法或者可能知道为什么它不打印以及我能做些什么?提前致谢!

最佳答案

print.data.frame(Variable) 有效。当然,我在发布它后两秒钟就想通了……但是花了好几个小时来弄乱这个。

关于r - Print() 函数不在循环/函数内打印变量(R 代码),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51849410/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com