【问题标题】:Use ´mapply´ to create an output [duplicate]使用“mapply”创建输出[重复]
【发布时间】:2020-05-15 15:00:39
【问题描述】:

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

数据

我的数据框中只有一行(也是唯一一行):

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】:

    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 
    

    出于这个原因,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 
    

    【讨论】:

      【解决方案2】:

      一种选择是将您的输出分配给一个变量:

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

      【讨论】:

      • 这可以完成工作。但是,现在我有一个无意义的变量x,它既没有任何有意义的内容,也不需要它。
      • 嗯,这只是一个选择
      • 当然。我无意批评你或你的回答。我的目的是为每个想知道哪种解决方案更适合他们的需求的人解释解决方案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-20
      • 2020-11-06
      相关资源
      最近更新 更多