删除R中apply语句和函数后面的逗号

2cmtqfgy  于 2023-02-06  发布在  其他
关注(0)|答案(1)|浏览(103)

当我使用apply语句来运行函数时,它输出的表用逗号分隔,我尝试了很多方法来看看是否能让逗号停止出现并不断失败。
重写了代码以提供示例...

---
output: pdf_document
---

```{r setup, include=FALSE, warning=FALSE}
knitr::opts_chunk$set( echo = FALSE , warning = FALSE , message = FALSE , cache = FALSE )

# Load libraries 
library( data.table )
library( kableExtra )
library( knitr )
library( ggplot2 )
# Recreate comma issue after sapply 

cols <- list( diamonds )
dfs <- list( diamonds )
jan <- data.table( diamonds ) 
cols_jan <- colnames( diamonds[ , c( 1:4 , 7 ) ])

tabs <- function( number , design , dts ){
          
          x <- y <- z <- NULL 
          dts <- jan
          vars <- cols_jan[number]
          
          out <- x <- y <- z <- NULL 
          x <- dts[ , .( counts = .N ) , by= vars ]
          x <- x[ order( x[ , 1 ] ) ,  ]
          x[ , `:=` ( Percent = ifelse( counts < 30 , NA , counts/nrow( dts ))) , ]
          row.names( x ) <- NULL 
          x[ , counts := ifelse( counts < 30 , NA , as.numeric( counts )) , ]
                
          z <- x 
          z[ , `:=` (
                  counts = scales::number( counts , accuracy = 1 , big.mark="," ) ,
                  Percent = scales::percent( Percent , accuracy = 0.1 ) 
          )]  
                   
          colnames( z ) <- c( ' ' , 'Counts' , 'Frequency' )
          out <- knitr::kable( z , format = 'latex', booktabs = TRUE )
          out
          
          }

r sapply( 2:4 , tabs )


需要留在R中,使用markdown,以PDF或Word输出。我使用apply语句,但愿意使用任何有效的语句。
看起来很简单,我觉得问这个问题很傻。感谢任何帮助-也确定这个函数可以用更少的代码重新创建...我不是在问这个,只是如何去掉每次运行之间的逗号(或者,在这个例子中,数字)。
"非常感谢"

![](https://i.stack.imgur.com/6FqyP.png)
xvw2m8pv

xvw2m8pv1#

不知何故,单独打印是这样工作的:

---
output: pdf_document
---

```{r setup, include=FALSE, warning=FALSE}
knitr::opts_chunk$set( echo = FALSE , warning = FALSE , message = FALSE , cache = FALSE )

# Load libraries 
library( data.table )
library( kableExtra )
library( knitr )
library( ggplot2 )
# Recreate comma issue after sapply 

cols <- list( diamonds )
dfs <- list( diamonds )
jan <- data.table( diamonds ) 
cols_jan <- colnames( diamonds[ , c( 1:4 , 7 ) ])

tabs <- function( number , design , dts ){
          
          x <- y <- z <- NULL 
          dts <- jan
          vars <- cols_jan[number]
          
          out <- x <- y <- z <- NULL 
          x <- dts[ , .( counts = .N ) , by= vars ]
          x <- x[ order( x[ , 1 ] ) ,  ]
          x[ , `:=` ( Percent = ifelse( counts < 30 , NA , counts/nrow( dts ))) , ]
          row.names( x ) <- NULL 
          x[ , counts := ifelse( counts < 30 , NA , as.numeric( counts )) , ]
                
          z <- x 
          z[ , `:=` (
                  counts = scales::number( counts , accuracy = 1 , big.mark="," ) ,
                  Percent = scales::percent( Percent , accuracy = 0.1 ) 
          )]  
                   
          colnames( z ) <- c( ' ' , 'Counts' , 'Frequency' )
          out <- knitr::kable( z , format = 'latex', booktabs = TRUE )
          out
          
          }

r tabs(2)
r tabs(3)
r tabs(4)


输出:

![](https://i.stack.imgur.com/rNlDM.png)

相关问题