R语言 舍入值时未生成表

9vw9lbht  于 2023-09-27  发布在  其他
关注(0)|答案(1)|浏览(58)

我试图创建下面的摘要,但在创建摘要时,子函数roundval给出了tabl1的错误,否则tabl2工作正常。
我不能改变数据,我必须处理NA,所以我应该在函数perc_75中更新什么以获得tab1的输出

library(expss)

dat <- data.frame(cc=c("AMB","CCU","DDI","GLL","MNA","KMB","LTI","DDI","GLL","MNA","CCU","AMB","KMB","LTI","DDI","CCU","GLL"),
                     duration=c(1,NA,66,NA,5,NA,3,1,NA,21,NA,25,NA,17,NA,NA,6),
                     duration2=c(1,4,66,5,5,11,3,1,6,21,54,25,12,17,9,8,6))

roundval <- function (x) 
{
  
  ifelse(round(abs(x - trunc(x)), 1) == 0.5, trunc(x + 0.5), 
         round(x))
}

perc_75 <- function(x) roundval(quantile(x,type = 6,probs = seq(0,1, 0.25),na.rm = TRUE))[4]

tabl1 <- cross_fun(
  dat,
  dat$duration,
  col_vars = dat$cc,
  fun = combine_functions(
    `75th Perc` = perc_75,
    `Valid N` = valid_n
  )
)

tabl2 <- cross_fun(
  dat,
  dat$duration2,
  col_vars = dat$cc,
  fun = combine_functions(
    `75th Perc` = perc_75,
    `Valid N` = valid_n
  )
)

下面是我得到的错误
[.data.table(raw_data,,fun(.SD),by = by_string)中的错误:组2的结果的第1列是“integer”类型,但应为“double”类型。每个组的列类型必须一致。

y3bcpkx1

y3bcpkx11#

您可以更改roundval,使其返回整数值:

roundval <- function (x) 
{
    
    ifelse(round(abs(x - trunc(x)), 1) == 0.5, as.integer(trunc(x + 0.5)), 
           as.integer(round(x)))
}

相关问题