4

I want to create an output with a bullet point for each entry

Data

I have one (and only one) row from my data frame:

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")

Approach

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

Desired Output

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

Actual Result

I am pretty close to what I want. However, R does not only print the desired part it adds the list of all columns afterwards. So the output looks like this:

*  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

More than just a solution that works I would very much appreciate if anyone can give me a hint what happens here.

Jan
  • 4,974
  • 3
  • 26
  • 43

2 Answers2

3

The *apply functions in R always have an output.

One way around this is to call them with 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 

The purrr package has the walk set of functions for just this reason:

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 
Ian Campbell
  • 23,484
  • 14
  • 36
  • 57
1

One option is to assign your output to a variable:

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

Bulat
  • 6,869
  • 1
  • 29
  • 52
  • This gets the job done. However, now I have a meaningless variable `x`that has neither any meaningful content nor do I need it. – Jan May 15 '20 at 15:28
  • well, that is just an option – Bulat May 15 '20 at 15:34
  • 2
    Of course. I did not intend to critizise you or your answer. My intention was to explain the solution for everybody who wonders which solution suits their needs better. – Jan May 15 '20 at 15:44