如何摆脱输出中的列表位置/行号
How to get rid of list positions / line numbers in an output
我想显示一个函数的许多结果,但我读到您只能 return 一个对象,因此如果您想显示更多,则必须使用列表。这工作正常但是输出有时不是很可读(在这个假的例子中它不是太糟糕,但在我的工作中它是)。我怎样才能摆脱或抑制 R 自动添加到我的输出中的这些 line/list 个位置?
当前输出:
[[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 的回答在这种情况下效果很好,因为我在这个例子中没有包含 list/dataframe。我已经更新了下面的函数以显示您在尝试使用一个礼物时遇到的错误,即;
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'
如果您想改进输出的打印方式,我们可以使用 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
编辑
如果我们只想显示进入 return
ing 的结果,我们可以从函数本身打印它们。
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
的东西所以它在那种情况下可能有用.
我想显示一个函数的许多结果,但我读到您只能 return 一个对象,因此如果您想显示更多,则必须使用列表。这工作正常但是输出有时不是很可读(在这个假的例子中它不是太糟糕,但在我的工作中它是)。我怎样才能摆脱或抑制 R 自动添加到我的输出中的这些 line/list 个位置?
当前输出:
[[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 的回答在这种情况下效果很好,因为我在这个例子中没有包含 list/dataframe。我已经更新了下面的函数以显示您在尝试使用一个礼物时遇到的错误,即;
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'
如果您想改进输出的打印方式,我们可以使用 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
编辑
如果我们只想显示进入 return
ing 的结果,我们可以从函数本身打印它们。
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
的东西所以它在那种情况下可能有用.