Nortest and generic variables

INPUT

#pV is a data set with 11 x variables x1,x2,...x11

for(i in 1:11){
v <- paste("x",i,sep="")
data=pV[[v]]

shap <- shapiro.test(data)
andersonDarling <- nortest::ad.test(data)
lilliefors <- nortest::lillie.test(data)
pearsonNormalityTest <- nortest::pearson.test(data)

print(shap)
print(andersonDarling)
print(lilliefors)
print(pearsonNormalityTest)
}

DESIRED OUTPUT

In all of the tests that follow I want data to be replaced by pVx1, pVx2,...pVx11 or x1,x2,...x11
This would make the output more meaningful rather than having data just be described as data.
How do I do that?

OUTPUT


	Shapiro-Wilk normality test

data:  data
W = 0.87896, p-value < 0.00000000000000022


	Anderson-Darling normality test

data:  data
A = 114.7, p-value < 0.00000000000000022


	Lilliefors (Kolmogorov-Smirnov) normality test

data:  data
D = 0.13643, p-value < 0.00000000000000022


	Pearson chi-square normality test

data:  data
P = 3088.3, p-value < 0.00000000000000022


	Shapiro-Wilk normality test

data:  data
W = 0.87732, p-value < 0.00000000000000022


	Anderson-Darling normality test

data:  data
A = 139.75, p-value < 0.00000000000000022


	Lilliefors (Kolmogorov-Smirnov) normality test

data:  data
D = 0.14681, p-value < 0.00000000000000022


	Pearson chi-square normality test

data:  data
P = 2414.1, p-value < 0.00000000000000022

etc.

It would be great to have data summarized as follows but that is a later question.

x Shapiro AndersonDarling Liliefors Pearson

x1 Shapiro AndersonDarling Liliefors Pearson
x2 Shapiro AndersonDarling Liliefors Pearson

is where the label comes from.

MyFabData <- runif(100, min = 2, max = 4)
shap <- shapiro.test(MyFabData)
print(shap)
#> 
#>  Shapiro-Wilk normality test
#> 
#> data:  MyFabData
#> W = 0.93402, p-value = 8.48e-05

I'll see what I can gin up for turning out results by variable name.

Thanks. I am following prior conversations on the use of for(i in c(1:11) and following code. This is a really powerful tool. MM

library(purrr)
library(glue)
library(rlang)

PV <- list(my_first_name_choice=iris$Petal.Length,
           my_second_name_choice=iris$Sepal.Length)

my_results <- imap(PV, \(val,nm) {
  assign(x = nm,
         value = val)
  myexp <- glue("shapiro.test(x = {nm})")
  
  eval(parse_expr(myexp))
  })

Although @nirgrahamuk 's solution works, I find {purrr} and {rlang} syntax difficult to read, let alone remember.

# unearth.R
# author: Richard Careaga
# Date: 2023-02-01
# illustration of functional programming to 
# running multiple statistical tests on
# variables in same data frame and having
# the resulting objects identify the 
# variables tested by the name of the
# variable

# libraries
library(nortest)

# data
# for reproducibility
set.seed(42)
# exemplary data
bucket <- sample(seq(0, 600, 0.1), 110)
pV <- data.frame(
  x1 = bucket[1:10],
  x2 = bucket[11:20],
  x3 = bucket[21:30],
  x4 = bucket[31:40],
  x5 = bucket[41:50],
  x6 = bucket[51:60],
  x7 = bucket[61:70],
  x8 = bucket[71:80],
  x9 = bucket[81:90],
  x10 = bucket[91:100],
  x11 = bucket[101:110]
)

# approach
# f(x) = y applied to data frame pV as x, containing
# x1 ... x11 to produce an object from which results
# of functions f1 .. f4 representing four tests for
# normality

# functions

# extract the name of a variable given as an 
# argument to a function through iteration
# over a data frame from the return value
# of the function

get_names <- function(x) names(x)

# wrapper function for the four included functions
# which perform the tests of interest based on
# one function from stats and three functions from
# nortest. The four functions are the substance
# of interest; other functions are for iteration
# and presentation

get_tests <- function(x){
  my_shapiro = function(x) shapiro.test(x)
  my_ad      = function(x) ad.test(x)
  my_lillie  = function(x) lillie.test(x)
  my_pearson = function(x) pearson.test(x)
  list(my_shapiro(x),my_ad(x),my_lillie(x),my_pearson(x))
}

make_tests <- function(){
  for(i in seq_along(output)) shapiros[i] = output[i][[1]][1]
  for(i in seq_along(output)) darlings[i] = output[i][[1]][2]
  for(i in seq_along(output)) lilles[i]   = output[i][[1]][3]
  for(i in seq_along(output)) pearsons[i] = output[i][[1]][4]
}
# preprocessing
# create a list object with the results in list form
# of each test on each variable

output <- lapply(pV,get_tests)
the_names <- get_names(output)

# main

# show values of the data.name before 
output[11]
#> $x11
#> $x11[[1]]
#> 
#>  Shapiro-Wilk normality test
#> 
#> data:  x
#> W = 0.92524, p-value = 0.4028
#> 
#> 
#> $x11[[2]]
#> 
#>  Anderson-Darling normality test
#> 
#> data:  x
#> A = 0.34416, p-value = 0.4084
#> 
#> 
#> $x11[[3]]
#> 
#>  Lilliefors (Kolmogorov-Smirnov) normality test
#> 
#> data:  x
#> D = 0.16802, p-value = 0.5867
#> 
#> 
#> $x11[[4]]
#> 
#>  Pearson chi-square normality test
#> 
#> data:  x
#> P = 3.2, p-value = 0.3618

# changes names of the htest$data.name objects
for(i in seq_along(output)) output[i][[1]][1][[1]]$data.name = the_names[i]
for(i in seq_along(output)) output[i][[1]][2][[1]]$data.name = the_names[i]
for(i in seq_along(output)) output[i][[1]][3][[1]]$data.name = the_names[i]
for(i in seq_along(output)) output[i][[1]][4][[1]]$data.name = the_names[i]


# and after
output[11]
#> $x11
#> $x11[[1]]
#> 
#>  Shapiro-Wilk normality test
#> 
#> data:  x11
#> W = 0.92524, p-value = 0.4028
#> 
#> 
#> $x11[[2]]
#> 
#>  Anderson-Darling normality test
#> 
#> data:  x11
#> A = 0.34416, p-value = 0.4084
#> 
#> 
#> $x11[[3]]
#> 
#>  Lilliefors (Kolmogorov-Smirnov) normality test
#> 
#> data:  x11
#> D = 0.16802, p-value = 0.5867
#> 
#> 
#> $x11[[4]]
#> 
#>  Pearson chi-square normality test
#> 
#> data:  x11
#> P = 3.2, p-value = 0.3618

Created on 2023-02-02 with reprex v2.0.2

I have gotten a little further. scientific notation is not working? Any thoughts?

Tables of Formal tests of normality of x1:x11 (raw)

pV <- read.csv("pV.csv", header=TRUE )

#create dataframe (table of tests of normality)

fine<-data.frame()

for(i in 1:11){
v <- paste("x",i,sep="")
test <- c("SW","AD","LIL","PEARSON")
testList <- data.frame(variable=rep(v,4),test=test, statistic=rep("",4), pvalue=rep("",4))

data=pV[[v]]

testList[1,3] <- round(shapiro.test(data)$statistic,3)
testList[1,4] <- shapiro.test(data)$p.value
testList[2,3] <- round(nortest::ad.test(data)$statistic,3)
testList[2,4] <- nortest::ad.test(data)$p.value
testList[3,3] <- round(nortest::lillie.test(data)$statistic,3)
testList[3,4] <- nortest::lillie.test(data)$p.value
testList[4,3] <- round(nortest ::pearson.test(data)$statistic,3)
testList[4,4] <- nortest ::pearson.test(data)$p.value

fine<-rbind(fine,testList)

formatC(testList[1,4], format = "e", digits = 4)     
formatC(testList[2,4], format = "e", digits = 4)      
formatC(testList[3,4], format = "e", digits = 4)     
formatC(testList[4,4], format = "e", digits = 4)     
}

# printed version, table pvalue is not readable, too many digits, not in scientific notation
print(fine)

I can only provide a general comment due to the lack of a truereprex. See the FAQ. Although pieces are here, the exact place throwing the error would have to be reverse engineer. Also, a reprex runs in a fresh session when produced, so there's nothing else in .Global to confuse things. But, not all is lost.

then_num <- 0.230397902792453
then_num |> str()
#>  num 0.23
now_char <- formatC(then_num,format = "e", digits = 4) 
now_char |> str()
#>  chr "2.3040e-01"

See what happened? A variable that was typeof double was converted by formatC() to typeof character.

Most of us grew up used to combining data with presentation—just print out the spreadsheet. But in formatting in R we usually change the nature of a data object so as to lose the ability to perform calculations on it without first converting it back, which isn't always possible. (That is, to be pedantic, the operation lacks idempotence.) Also, when this is attempted to part of a data frame column, it flips the type of all numeric rows to character.

What to do? Use a formatter

library(gt)
library(knitr)
d <- data.frame(original = 0.230397902792453)
d |> gt()
html { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Helvetica Neue', 'Fira Sans', 'Droid Sans', Arial, sans-serif; } #ehzmsyuslj .gt_table { display: table; border-collapse: collapse; margin-left: auto; margin-right: auto; color: #333333; font-size: 16px; font-weight: normal; font-style: normal; background-color: #FFFFFF; width: auto; border-top-style: solid; border-top-width: 2px; border-top-color: #A8A8A8; border-right-style: none; border-right-width: 2px; border-right-color: #D3D3D3; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #A8A8A8; border-left-style: none; border-left-width: 2px; border-left-color: #D3D3D3; } #ehzmsyuslj .gt_heading { background-color: #FFFFFF; text-align: center; border-bottom-color: #FFFFFF; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; } #ehzmsyuslj .gt_caption { padding-top: 4px; padding-bottom: 4px; } #ehzmsyuslj .gt_title { color: #333333; font-size: 125%; font-weight: initial; padding-top: 4px; padding-bottom: 4px; padding-left: 5px; padding-right: 5px; border-bottom-color: #FFFFFF; border-bottom-width: 0; } #ehzmsyuslj .gt_subtitle { color: #333333; font-size: 85%; font-weight: initial; padding-top: 0; padding-bottom: 6px; padding-left: 5px; padding-right: 5px; border-top-color: #FFFFFF; border-top-width: 0; } #ehzmsyuslj .gt_bottom_border { border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D3D3D3; } #ehzmsyuslj .gt_col_headings { border-top-style: solid; border-top-width: 2px; border-top-color: #D3D3D3; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D3D3D3; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; } #ehzmsyuslj .gt_col_heading { color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: normal; text-transform: inherit; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; vertical-align: bottom; padding-top: 5px; padding-bottom: 6px; padding-left: 5px; padding-right: 5px; overflow-x: hidden; } #ehzmsyuslj .gt_column_spanner_outer { color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: normal; text-transform: inherit; padding-top: 0; padding-bottom: 0; padding-left: 4px; padding-right: 4px; } #ehzmsyuslj .gt_column_spanner_outer:first-child { padding-left: 0; } #ehzmsyuslj .gt_column_spanner_outer:last-child { padding-right: 0; } #ehzmsyuslj .gt_column_spanner { border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D3D3D3; vertical-align: bottom; padding-top: 5px; padding-bottom: 5px; overflow-x: hidden; display: inline-block; width: 100%; } #ehzmsyuslj .gt_group_heading { padding-top: 8px; padding-bottom: 8px; padding-left: 5px; padding-right: 5px; color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: initial; text-transform: inherit; border-top-style: solid; border-top-width: 2px; border-top-color: #D3D3D3; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D3D3D3; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; vertical-align: middle; text-align: left; } #ehzmsyuslj .gt_empty_group_heading { padding: 0.5px; color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: initial; border-top-style: solid; border-top-width: 2px; border-top-color: #D3D3D3; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D3D3D3; vertical-align: middle; } #ehzmsyuslj .gt_from_md > :first-child { margin-top: 0; } #ehzmsyuslj .gt_from_md > :last-child { margin-bottom: 0; } #ehzmsyuslj .gt_row { padding-top: 8px; padding-bottom: 8px; padding-left: 5px; padding-right: 5px; margin: 10px; border-top-style: solid; border-top-width: 1px; border-top-color: #D3D3D3; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; vertical-align: middle; overflow-x: hidden; } #ehzmsyuslj .gt_stub { color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: initial; text-transform: inherit; border-right-style: solid; border-right-width: 2px; border-right-color: #D3D3D3; padding-left: 5px; padding-right: 5px; } #ehzmsyuslj .gt_stub_row_group { color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: initial; text-transform: inherit; border-right-style: solid; border-right-width: 2px; border-right-color: #D3D3D3; padding-left: 5px; padding-right: 5px; vertical-align: top; } #ehzmsyuslj .gt_row_group_first td { border-top-width: 2px; } #ehzmsyuslj .gt_summary_row { color: #333333; background-color: #FFFFFF; text-transform: inherit; padding-top: 8px; padding-bottom: 8px; padding-left: 5px; padding-right: 5px; } #ehzmsyuslj .gt_first_summary_row { border-top-style: solid; border-top-color: #D3D3D3; } #ehzmsyuslj .gt_first_summary_row.thick { border-top-width: 2px; } #ehzmsyuslj .gt_last_summary_row { padding-top: 8px; padding-bottom: 8px; padding-left: 5px; padding-right: 5px; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D3D3D3; } #ehzmsyuslj .gt_grand_summary_row { color: #333333; background-color: #FFFFFF; text-transform: inherit; padding-top: 8px; padding-bottom: 8px; padding-left: 5px; padding-right: 5px; } #ehzmsyuslj .gt_first_grand_summary_row { padding-top: 8px; padding-bottom: 8px; padding-left: 5px; padding-right: 5px; border-top-style: double; border-top-width: 6px; border-top-color: #D3D3D3; } #ehzmsyuslj .gt_striped { background-color: rgba(128, 128, 128, 0.05); } #ehzmsyuslj .gt_table_body { border-top-style: solid; border-top-width: 2px; border-top-color: #D3D3D3; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D3D3D3; } #ehzmsyuslj .gt_footnotes { color: #333333; background-color: #FFFFFF; border-bottom-style: none; border-bottom-width: 2px; border-bottom-color: #D3D3D3; border-left-style: none; border-left-width: 2px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 2px; border-right-color: #D3D3D3; } #ehzmsyuslj .gt_footnote { margin: 0px; font-size: 90%; padding-left: 4px; padding-right: 4px; padding-left: 5px; padding-right: 5px; } #ehzmsyuslj .gt_sourcenotes { color: #333333; background-color: #FFFFFF; border-bottom-style: none; border-bottom-width: 2px; border-bottom-color: #D3D3D3; border-left-style: none; border-left-width: 2px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 2px; border-right-color: #D3D3D3; } #ehzmsyuslj .gt_sourcenote { font-size: 90%; padding-top: 4px; padding-bottom: 4px; padding-left: 5px; padding-right: 5px; } #ehzmsyuslj .gt_left { text-align: left; } #ehzmsyuslj .gt_center { text-align: center; } #ehzmsyuslj .gt_right { text-align: right; font-variant-numeric: tabular-nums; } #ehzmsyuslj .gt_font_normal { font-weight: normal; } #ehzmsyuslj .gt_font_bold { font-weight: bold; } #ehzmsyuslj .gt_font_italic { font-style: italic; } #ehzmsyuslj .gt_super { font-size: 65%; } #ehzmsyuslj .gt_footnote_marks { font-style: italic; font-weight: normal; font-size: 75%; vertical-align: 0.4em; } #ehzmsyuslj .gt_asterisk { font-size: 100%; vertical-align: 0; } #ehzmsyuslj .gt_indent_1 { text-indent: 5px; } #ehzmsyuslj .gt_indent_2 { text-indent: 10px; } #ehzmsyuslj .gt_indent_3 { text-indent: 15px; } #ehzmsyuslj .gt_indent_4 { text-indent: 20px; } #ehzmsyuslj .gt_indent_5 { text-indent: 25px; }
original
0.2303979
d |> gt() |> fmt_scientific(columns = original, decimals = 4)
html { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Helvetica Neue', 'Fira Sans', 'Droid Sans', Arial, sans-serif; } #ouobxzjjyi .gt_table { display: table; border-collapse: collapse; margin-left: auto; margin-right: auto; color: #333333; font-size: 16px; font-weight: normal; font-style: normal; background-color: #FFFFFF; width: auto; border-top-style: solid; border-top-width: 2px; border-top-color: #A8A8A8; border-right-style: none; border-right-width: 2px; border-right-color: #D3D3D3; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #A8A8A8; border-left-style: none; border-left-width: 2px; border-left-color: #D3D3D3; } #ouobxzjjyi .gt_heading { background-color: #FFFFFF; text-align: center; border-bottom-color: #FFFFFF; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; } #ouobxzjjyi .gt_caption { padding-top: 4px; padding-bottom: 4px; } #ouobxzjjyi .gt_title { color: #333333; font-size: 125%; font-weight: initial; padding-top: 4px; padding-bottom: 4px; padding-left: 5px; padding-right: 5px; border-bottom-color: #FFFFFF; border-bottom-width: 0; } #ouobxzjjyi .gt_subtitle { color: #333333; font-size: 85%; font-weight: initial; padding-top: 0; padding-bottom: 6px; padding-left: 5px; padding-right: 5px; border-top-color: #FFFFFF; border-top-width: 0; } #ouobxzjjyi .gt_bottom_border { border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D3D3D3; } #ouobxzjjyi .gt_col_headings { border-top-style: solid; border-top-width: 2px; border-top-color: #D3D3D3; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D3D3D3; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; } #ouobxzjjyi .gt_col_heading { color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: normal; text-transform: inherit; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; vertical-align: bottom; padding-top: 5px; padding-bottom: 6px; padding-left: 5px; padding-right: 5px; overflow-x: hidden; } #ouobxzjjyi .gt_column_spanner_outer { color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: normal; text-transform: inherit; padding-top: 0; padding-bottom: 0; padding-left: 4px; padding-right: 4px; } #ouobxzjjyi .gt_column_spanner_outer:first-child { padding-left: 0; } #ouobxzjjyi .gt_column_spanner_outer:last-child { padding-right: 0; } #ouobxzjjyi .gt_column_spanner { border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D3D3D3; vertical-align: bottom; padding-top: 5px; padding-bottom: 5px; overflow-x: hidden; display: inline-block; width: 100%; } #ouobxzjjyi .gt_group_heading { padding-top: 8px; padding-bottom: 8px; padding-left: 5px; padding-right: 5px; color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: initial; text-transform: inherit; border-top-style: solid; border-top-width: 2px; border-top-color: #D3D3D3; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D3D3D3; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; vertical-align: middle; text-align: left; } #ouobxzjjyi .gt_empty_group_heading { padding: 0.5px; color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: initial; border-top-style: solid; border-top-width: 2px; border-top-color: #D3D3D3; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D3D3D3; vertical-align: middle; } #ouobxzjjyi .gt_from_md > :first-child { margin-top: 0; } #ouobxzjjyi .gt_from_md > :last-child { margin-bottom: 0; } #ouobxzjjyi .gt_row { padding-top: 8px; padding-bottom: 8px; padding-left: 5px; padding-right: 5px; margin: 10px; border-top-style: solid; border-top-width: 1px; border-top-color: #D3D3D3; border-left-style: none; border-left-width: 1px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 1px; border-right-color: #D3D3D3; vertical-align: middle; overflow-x: hidden; } #ouobxzjjyi .gt_stub { color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: initial; text-transform: inherit; border-right-style: solid; border-right-width: 2px; border-right-color: #D3D3D3; padding-left: 5px; padding-right: 5px; } #ouobxzjjyi .gt_stub_row_group { color: #333333; background-color: #FFFFFF; font-size: 100%; font-weight: initial; text-transform: inherit; border-right-style: solid; border-right-width: 2px; border-right-color: #D3D3D3; padding-left: 5px; padding-right: 5px; vertical-align: top; } #ouobxzjjyi .gt_row_group_first td { border-top-width: 2px; } #ouobxzjjyi .gt_summary_row { color: #333333; background-color: #FFFFFF; text-transform: inherit; padding-top: 8px; padding-bottom: 8px; padding-left: 5px; padding-right: 5px; } #ouobxzjjyi .gt_first_summary_row { border-top-style: solid; border-top-color: #D3D3D3; } #ouobxzjjyi .gt_first_summary_row.thick { border-top-width: 2px; } #ouobxzjjyi .gt_last_summary_row { padding-top: 8px; padding-bottom: 8px; padding-left: 5px; padding-right: 5px; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D3D3D3; } #ouobxzjjyi .gt_grand_summary_row { color: #333333; background-color: #FFFFFF; text-transform: inherit; padding-top: 8px; padding-bottom: 8px; padding-left: 5px; padding-right: 5px; } #ouobxzjjyi .gt_first_grand_summary_row { padding-top: 8px; padding-bottom: 8px; padding-left: 5px; padding-right: 5px; border-top-style: double; border-top-width: 6px; border-top-color: #D3D3D3; } #ouobxzjjyi .gt_striped { background-color: rgba(128, 128, 128, 0.05); } #ouobxzjjyi .gt_table_body { border-top-style: solid; border-top-width: 2px; border-top-color: #D3D3D3; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #D3D3D3; } #ouobxzjjyi .gt_footnotes { color: #333333; background-color: #FFFFFF; border-bottom-style: none; border-bottom-width: 2px; border-bottom-color: #D3D3D3; border-left-style: none; border-left-width: 2px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 2px; border-right-color: #D3D3D3; } #ouobxzjjyi .gt_footnote { margin: 0px; font-size: 90%; padding-left: 4px; padding-right: 4px; padding-left: 5px; padding-right: 5px; } #ouobxzjjyi .gt_sourcenotes { color: #333333; background-color: #FFFFFF; border-bottom-style: none; border-bottom-width: 2px; border-bottom-color: #D3D3D3; border-left-style: none; border-left-width: 2px; border-left-color: #D3D3D3; border-right-style: none; border-right-width: 2px; border-right-color: #D3D3D3; } #ouobxzjjyi .gt_sourcenote { font-size: 90%; padding-top: 4px; padding-bottom: 4px; padding-left: 5px; padding-right: 5px; } #ouobxzjjyi .gt_left { text-align: left; } #ouobxzjjyi .gt_center { text-align: center; } #ouobxzjjyi .gt_right { text-align: right; font-variant-numeric: tabular-nums; } #ouobxzjjyi .gt_font_normal { font-weight: normal; } #ouobxzjjyi .gt_font_bold { font-weight: bold; } #ouobxzjjyi .gt_font_italic { font-style: italic; } #ouobxzjjyi .gt_super { font-size: 65%; } #ouobxzjjyi .gt_footnote_marks { font-style: italic; font-weight: normal; font-size: 75%; vertical-align: 0.4em; } #ouobxzjjyi .gt_asterisk { font-size: 100%; vertical-align: 0; } #ouobxzjjyi .gt_indent_1 { text-indent: 5px; } #ouobxzjjyi .gt_indent_2 { text-indent: 10px; } #ouobxzjjyi .gt_indent_3 { text-indent: 15px; } #ouobxzjjyi .gt_indent_4 { text-indent: 20px; } #ouobxzjjyi .gt_indent_5 { text-indent: 25px; }
original
2.3040 × 10−1
``` which trnaslate to

Screenshot 2023-02-18 at 2.36.49 PM

# fussier
formatC(d$original, format = "e", digits = 4) |>   kable()

| x          |
|:-----------|
| 2.3040e-01 |

One thing to keep in mind, though— it's easy to spend time on how it looks rather than what it says.

Thank you, this will take some study. I came to the same conclusion about numeric to character. I like creating tables so I need to come up with something generic like always output character and then convert back to numeric. I'm just not sure that level of generic processing is practical across many different functions and mathematical operations.

I need to prepare a better question. I think the primary indicator here of there being a problem with mixed datatypes is when I combine variables of different datatypes to form a table. That's when the real problem occurs.I need to focus my solution there.

I'm not sure what kable() does?

Thanks again for the reply.

Sincerely,
Mary

Hi, Mary

Usually, we're working with a data frame so that character and numeric data can be mixed. In a matrix they have to be one or the other—they can't be mixed.

Be protective of a data frame that has been prepared for the purposes of scripting. Use it as an argument to a formatting function in some package. Or at least work with a copy, because every conversion of an entry from one typeof and then back opens a window for error. If you really want to get fine-grained with tables there {gt} package is good. The only shortcoming is for the specific purpose of formatting as 1.23e\text{-}1 instead of 1.23\times10^{-1}. Other than that, it's what I'd recommend for general use with an HTML target.

For pdf, there's {xtable} to open up full LaTeX where anything is possible, although not always easily.

g(x) = ln\Bigl[\frac{\pi(x)6}{1 - \pi(x)}\Big] = \beta_0 +\beta_1x

And, for some purposes https://cran.r-project.org/web/packages/stargazer/vignettes/stargazer.pdf and, IIRC, a number of others.

1 Like

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.