gpt4 book ai didi

r - 使用“mapply”创建输出

转载 作者:行者123 更新时间:2023-12-03 23:07:09 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





How to suppress part of the output from `lapply()`?

(2 个回答)


去年关闭。




我想为每个条目创建一个带有项目符号的输出

数据

我的数据框中有一行(并且只有一行):

structure(list(Dimensions = 2L, Continuity = structure(2L, .Label = c("", 
"continuous"), class = "factor"), Differentiability = structure(2L, .Label = c("",
"differentiable", "non-differentiable"), class = "factor"), Convexity = structure(2L, .Label = c("",
"convex", "non-convex"), class = "factor"), Modality = structure(3L, .Label = c("",
"multimodal", "unimodal"), class = "factor"), Separability = structure(2L, .Label = c("",
"non-separable", "non-separable,", "separable"), class = "factor"),
Scalability = structure(2L, .Label = c("", "non-scalable",
"scalable"), class = "factor"), Parametric = FALSE, Random = FALSE), row.names = 2L, class = "data.frame")

方法
mapply(function(x, y) cat("* ", y, ": ", as.character(x), "\n"), Descr, names(Descr))

期望输出
* Dimensions :  2
* Continuity : continuous
* Differentiability : differentiable
* Convexity : convex
* Modality : unimodal
* Separability : non-separable
* Scalability : non-scalable
* Parametric : FALSE
* Random : FALSE

实际结果

我非常接近我想要的。但是,R 不仅打印所需的部分,它随后还会添加所有列的列表。所以输出看起来像这样:
*  Dimensions :  2 
* Continuity : continuous
* Differentiability : differentiable
* Convexity : convex
* Modality : unimodal
* Separability : non-separable
* Scalability : non-scalable
* Parametric : FALSE
* Random : FALSE
$Dimensions
NULL

$Continuity
NULL

$Differentiability
NULL

$Convexity
NULL

$Modality
NULL

$Separability
NULL

$Scalability
NULL

$Parametric
NULL

$Random
NULL

不仅仅是一个有效的解决方案,如果有人能给我提示这里发生了什么,我将非常感激。

最佳答案

*apply R 中的函数总是有输出。

解决此问题的一种方法是使用 invisible 调用它们。 :

invisible(mapply(function(x, y) cat("* ", y, ": ", as.character(x), "\n"), Descr, names(Descr)))
* Dimensions : 2
* Continuity : continuous
* Differentiability : differentiable
* Convexity : convex
* Modality : unimodal
* Separability : non-separable
* Scalability : non-scalable
* Parametric : FALSE
* Random : FALSE
purrr包有 walk正是出于这个原因的一组函数:
library(purrr)
walk2(Descr,names(Descr), function(x, y) cat("* ", y, ": ", as.character(x), "\n"))
* Dimensions : 2
* Continuity : continuous
* Differentiability : differentiable
* Convexity : convex
* Modality : unimodal
* Separability : non-separable
* Scalability : non-scalable
* Parametric : FALSE
* Random : FALSE

关于r - 使用“mapply”创建输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61822263/

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