使用“ mapply”创建输出

问题描述 投票:1回答:1

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

数据

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

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

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

r mapply
1个回答
0
投票

R中的*apply函数始终具有输出。

解决此问题的一种方法是使用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 
© www.soinside.com 2019 - 2024. All rights reserved.