r 代码段显示了自定义的HTML表函数和示例。我的下一步是将此HTML表复制并粘贴到excel文件中,

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了r 代码段显示了自定义的HTML表函数和示例。我的下一步是将此HTML表复制并粘贴到excel文件中,相关的知识,希望对你有一定的参考价值。

# Quite a few requirements so read closely
# - variable must filter out NAs
# - variable must be a factor
# - variable must have factor labels (i think)
# - must be slightly comfortable with dplyr

# FUNCTION
tbl_steele <- function(data, names){
  require(htmlTable)
  a <- lapply(data, function(x) freq(x, plot = F))
  b <- do.call(rbind, a)
  remove_total <- "Total"
  d <- b[!rownames(b) %in% remove_total, ]
  e <- data.frame(d)
  e_prettyNum <- prettyNum(e$Frequency, big.mark = ",", scientific = FALSE, preserve.width = "none")
  f <- cbind(paste(e_prettyNum, " ", "(", round(e$Percent, 1), ")", sep = ""))
  g <- lapply(data, function(x) length(levels(x)))
  g1 <- do.call(rbind, g)
  h <- htmlTable(f, rnames = rownames(d), rgroup = names, n.rgroup = c(g1))
  return(h)
}

# EXAMPLE
# requires dplyr as I use the filter function to filter out NAs as function will not properly run
# Again, filter out NAs before passing varaible through function
mtcars %>% 
  select(gear) %>% 
  transmute(gear_fctr = factor(gear, 
                             levels = c(3, 4, 5), 
                             labels = c("3. Three", "4. Four", "5. Five"))) %>% 
  tbl_steele(data = ., names = names(.))

# COUNTER EXAMPLE TO "UNFACTORED" VARIABLE
# Change transmute to mutate and you'll see the weird behavior
mtcars %>% 
  select(gear) %>% 
  mutate(gear_fctr = factor(gear, 
                             levels = c(3, 4, 5), 
                             labels = c("3. Three", "4. Four", "5. Five"))) %>% 
  tbl_steele(data = ., names = names(.))
  
# function is in its infancy, hence the strict requirements (sorry)

以上是关于r 代码段显示了自定义的HTML表函数和示例。我的下一步是将此HTML表复制并粘贴到excel文件中,的主要内容,如果未能解决你的问题,请参考以下文章

按钮未显示在android的自定义列表视图中

R plotly禁用默认悬停文本并仅显示自定义文本

函数复习

R Keras 中的自定义损失函数

模板类函数的未定义错误

R语言使用table1包绘制(生成)三线表使用单变量分列构建三线表编写自定义函数通过t检验为三线表添加p值列设置不显示overall数值