【问题标题】:How to get rid of list positions / line numbers in an output如何摆脱输出中的列表位置/行号
【发布时间】:2018-10-17 05:23:38
【问题描述】:

我想显示一个函数的许多结果,但我读到你只能返回一个对象,因此如果你想显示更多,就必须使用一个列表。这工作正常,但有时输出不是很可读(在这个假例子中,它并不算太糟糕,但在我的工作中是这样)。如何摆脱或抑制 R 自动添加到我的输出中的这些行/列表位置?

当前输出:

[[1]]
[1] "There are 5 total observations"

[[2]]
[1] "The mean of these observations is 0.564422113896047"

[[3]]
[1] "The observations are shown below:"

[[4]]
[1]  1.0496648  0.4807251  0.8536269  1.7946839 -1.3565901

期望的输出:

"There are 5 total observations"


"The mean of these observations is 0.564422113896047"


"The observations are shown below:"


 1.0496648  0.4807251  0.8536269  1.7946839 -1.3565901

我很高兴能够移除每行上方的双括号输出,但保留行号输出。如果我还可以更改各个点的行距,那会更好,但并不是真正需要的。

用于创建此函数/输出的代码:

test <- function(n_observations) {

  obs <- rnorm(n_observations)

return(list(
  paste0("There are ",n_observations," total observations"),
  paste0("The mean of these observations is ",mean(obs)),
  paste0("The observations are shown below:"),
  obs
))

}

test(n_observations = 5)

编辑: Ronaks 的回答在这种情况下工作得很好,因为我在这个例子中没有包含列表/数据框。我已经更新了下面的函数,以显示您在使用一个礼物时遇到的错误,即;

 test <- function(n_observations) {

  obs <- rnorm(n_observations)
  random_table <- as.data.frame(cbind(c(1:n_observations), obs))

return(cat(
  paste0("There are ",n_observations," total observations\n"),
  paste("\n"),
  paste0("The mean of these observations is ",mean(obs),"\n"),
  paste0("The observations are shown below:\n"),
  obs,
  random_table
  ))
}

test(n_observations = 5)

输出(和错误):

There are 5 total observations

 The mean of these observations is 0.445438123798109
 The observations are shown below:
 1.677665 1.379066 0.3436419 0.4783038 -1.651487 Error in cat(paste0("There are ", n_observations, " total observations\n"),  : 
  argument 6 (type 'list') cannot be handled by 'cat'

【问题讨论】:

    标签: r list function readability


    【解决方案1】:

    如果您想改进输出的打印方式,我们可以使用cat,包括每行后的“\n”以在不同的行中显示输出。

    test <- function(n_observations) {
       obs <- rnorm(n_observations)
    
       return(cat(
         paste0("There are ",n_observations," total observations\n"),
         paste0("The mean of these observations is ",mean(obs), "\n"),
         paste0("The observations are shown below:\n"),
         obs
       ))
    }
    
    test(n_observations = 5)
    
    #There are 5 total observations
    # The mean of these observations is -0.785794194405614
    # The observations are shown below:
    # -0.4806757 -0.6366636 0.3147989 -1.873661 -1.25277
    

    编辑

    如果我们只想显示结果为什么要进入returning 他们,我们可以从函数本身打印它们。

    test <- function(n_observations) {
    
      obs <- rnorm(n_observations)
      random_table <- as.data.frame(cbind(c(1:n_observations), obs))
    
    
      cat(paste0("There are ",n_observations," total observations\n"),
      paste0("The mean of these observations is ",mean(obs), "\n"),
      paste0("The observations are shown below:\n"),
      obs, "\n\n The table is as below : \n\n")
      print(random_table)
    }
    
    test(n_observations = 5)
    
    #There are 5 total observations
    # The mean of these observations is 0.540141211615552
    # The observations are shown below:
    # 1.922104 -0.5334201 -0.9881913 1.838563 0.4616504 
    
    # The table is as below : 
    
    #  V1        obs
    #1  1  1.9221042
    #2  2 -0.5334201
    #3  3 -0.9881913
    #4  4  1.8385628
    #5  5  0.4616504
    

    可以避免最后一个print(random_table),我们只能使用random_table,但我假设OP对print有很多其他类似的东西,所以在这种情况下它可能很有用。

    【讨论】:

    • 谢谢你的两个例子,是的,我有很多不同的表格要显示,但你的方法很好地适用于此。
    猜你喜欢
    • 1970-01-01
    • 2023-04-05
    • 2017-01-24
    • 1970-01-01
    • 1970-01-01
    • 2017-02-15
    • 2015-03-04
    • 2016-01-12
    • 1970-01-01
    相关资源
    最近更新 更多