Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何进行垂直输出而不是水平输出?_R_Output Formatting - Fatal编程技术网

R 如何进行垂直输出而不是水平输出?

R 如何进行垂直输出而不是水平输出?,r,output-formatting,R,Output Formatting,我试图将某个特定函数的水平输出转换为垂直输出,我试图用“cat”函数来解决这个问题,但找不到这样做的方法。我所关注的功能如下: context <- function (a,b,c) { U=matrix(0, nrow = c, ncol = c) t = 0.4 m = 0.2 for (i in 1:c) { U[i] = t*a + b/i - 0.224 } cat(U) } context(2,3,7) 但是,我不知道如何通过操纵函数本身以垂直方式进

我试图将某个特定函数的水平输出转换为垂直输出,我试图用“cat”函数来解决这个问题,但找不到这样做的方法。我所关注的功能如下:

 context <- function (a,b,c)
 {
 U=matrix(0, nrow = c, ncol = c)
 t = 0.4
 m = 0.2
 for (i in 1:c)
 { 
 U[i] = t*a + b/i - 0.224
 }
 cat(U)
 }
 context(2,3,7)
但是,我不知道如何通过操纵函数本身以垂直方式进行此输出。 如果我能在这方面得到帮助,我将不胜感激


提前非常感谢

我不明白你为什么要创建所有这些零

也许你想返回一个矩阵

context <- function (a,b,c)
{
  t = 0.4
  m = 0.2
  U = t*a + b/seq_len(c) - 0.224
  as.matrix(U)
}

context(2,3,7)

#         [,1]
#[1,] 3.576000
#[2,] 2.076000
#[3,] 1.576000
#[4,] 1.326000
#[5,] 1.176000
#[6,] 1.076000
#[7,] 1.004571
context
t(数据)
可能是更好的解决方案


有关更多详细信息,请检查:。

为什么不使用函数return
U
而不是使用
cat
?太棒了!这正是我需要做的!我不知道我需要“as.matrix”公式!非常感谢你!
context <- function (a,b,c)
{
  t = 0.4
  m = 0.2
  U = t*a + b/seq_len(c) - 0.224
  as.matrix(U)
}

context(2,3,7)

#         [,1]
#[1,] 3.576000
#[2,] 2.076000
#[3,] 1.576000
#[4,] 1.326000
#[5,] 1.176000
#[6,] 1.076000
#[7,] 1.004571