I'm trying to round all numerical values in my data frame.
The issue is that my data frame also includes strings, and not just in any particular column or row. I want to avoid having to code a loop where I go through each individual row-column cell pair and check if the value is numerical before rounding.
Is there a function (or a combination of functions) that will let me achieve this?
So far I've tried round_df()
and various lapply()
and apply()
combinations with lambdas. However, I've only gotten where it rounds based on the first value in the column (i.e. if the first value is numerical, it treats the entire column as numerical and only rounds it).
I've run into problems then where the first value is a string and so the entire column goes un-rounded or vice-versa, in which my code errors because it tries to round a string.
My function is:
library(readxl)
library(knitr)
library(gplots)
library(doBy)
library(dplyr)
library(plyr)
library(printr)
library(xtable)
library(gmodels)
library(survival)
library(pander)
library(psych)
library(questionr)
library(DT)
library(data.table)
library(expss)
library(xtable)
options(xtable.floating = FALSE)
options(xtable.timestamp = "")
library(kableExtra)
library(magrittr)
library(Hmisc)
library(forestmangr)
library(summarytools)
library(gmodels)
library(stats)
summaryTable <- function(y, bygroup, digit,
title="", caption_heading="", caption="", freq.tab, y.label="",
y.names="", boxplot) {
if (freq.tab) {
m = multi.fun(y)
}
else if (!missing(bygroup)) {
m = data.frame(y.label = "")
m = merge(m, data.frame(describeBy(y, bygroup, mat = T)))
m = select(m, y.label, n, mean, sd, min, median, max)
}
else {
m = data.frame(y.label = "")
m = merge(m, data.frame(sumconti(y)))
}
if (!freq.tab) {
m$y.label = y.names
}
m = round_df(m, digit, "signif")
if (freq.tab) {
colnames(m) = c(y.label, "Frequency", "%")
}
else if (missing(freq.tab) | !freq.tab) {
colnames(m) = c(y.label, "n", "Mean", "Std", "Min", "Median", "Max")
}
if (!missing(boxplot)) {
if (boxplot) {
attach(m)
layout(matrix(c(1, 1, 2, 1)), 2, 1)
kable(m, align = "c", "latex", booktabs = T, caption=figTitle(x, title, y.label)) %>%
kable_styling(position = 'center',
latex_options = c("striped", "repeat_header", "hold_position")) %>%
footnote(general = caption, general_title = caption_heading, footnote_as_chunk = T,
title_format = c("italic", "underline"), threeparttable = T)
boxplot(y ~ bygroup, main = figTitle(y, title, y.label), names = y.names, ylab = title,
xlab = y.label, col = c("red", "blue", "orange", "pink",
"green", "purple", "grey", "yellow"), border = "black",
horizontal = F, varwidth = T)
}
}
kable(m,
align = "c",
"latex",
booktabs = T,
caption = figTitle(x, title, y.label)) %>%
kable_styling(position = 'center',
latex_options = c("striped", "repeat_header", "hold_position")) %>%
footnote(general = caption,
general_title = caption_heading,
footnote_as_chunk = T,
title_format = c("italic", "underline"),
threeparttable = T)
}
figTitle = function(x, title, y.label) {
if (y.label != "") {
paste("Summary of", title, "by", y.label)
}
else if (title != "") {
paste("Summary of", title)
}
else {
paste("")
}
}