首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用“”创建输出

使用“”创建输出
EN

Stack Overflow用户
提问于 2020-05-15 15:00:39
回答 2查看 52关注 0票数 4

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

数据

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

代码语言:javascript
复制
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")

方法

代码语言:javascript
复制
mapply(function(x, y) cat("* ", y, ": ", as.character(x), "\n"), Descr, names(Descr))

期望输出

代码语言:javascript
复制
* Dimensions :  2
* Continuity :  continuous
* Differentiability :  differentiable
* Convexity :  convex
* Modality :  unimodal
* Separability :  non-separable
* Scalability :  non-scalable
* Parametric :  FALSE
* Random :  FALSE

实际效果

我很接近我想要的。但是,R不只是打印想要的部分,而是在后面添加所有列的列表。输出结果如下:

代码语言:javascript
复制
*  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

不仅仅是一个有效的解决方案,如果有人能给我一个提示,我会非常感激的。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-05-15 15:10:47

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

一种方法就是用invisible给他们打电话。

代码语言:javascript
复制
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函数集:

代码语言:javascript
复制
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 
票数 3
EN

Stack Overflow用户

发布于 2020-05-15 15:15:11

一个选项是将输出分配给一个变量:

代码语言:javascript
复制
x <- mapply(function(x, y) cat("* ", y, ": ", as.character(x), "\n"), Descr, names(Descr))
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61822263

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档