我有一个关于在 R 笔记本中内联输出数字的问题。我想使用 dplyr 和管道切换到更易读的代码,但现在我想要计算的数字不再与文本一致。
到目前为止,我写了这样的代码:
Number of dogs: `r nrow(animals[which(animals$species == "dog"),])`!
我得到了内联数字:
Number of dogs: 8!
如果我切换到
Number of dogs: `r animals %>% filter(species == "dog") %>% count()`!
输出不再是内联的,而是插入到下面的行中,周围有一个框:
Number of dogs:
[ n]
[ <int>]
[ 90]
[1 row ]
!
如何取回内联输出?
结果被强制转换为tibble
.
library(dplyr)
(xy <- iris %>% filter(Species == "setosa") %>% count())
# A tibble: 1 × 1
n
<int>
1 50
将它包装成as.numeric
一个数字(长度为 1 的向量)。
> as.numeric(xy)
[1] 50
unlist(xy)
也有效。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句