Scientific notation does not always take effect when using formatC statement? How to do?

Scientific notation does not always take effect?

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

mydat <-read.table(header=T,text='
id	x1	x2	x3	x4	x5	x6	x7	x8	x9	x10	x11	y2	y
1	7.4	0.66	0	1.8	0.075	13	40	0.9978	3.51	0.56	9.4	5	0
2	7.8	0.58	0.02	2	0.073	9	18	0.9968	3.36	0.57	9.5	7	1
3	6.7	0.58	0.08	1.8	0.097	15	65	0.9959	3.28	0.54	9.2	5	0
4	8.9	0.62	0.18	3.8	0.176	52	145	0.9986	3.16	0.88	9.2	5	0
5	8.1	0.56	0.28	1.7	0.368	16	56	0.9968	3.11	1.28	9.3	5	0
6	7.4	0.59	0.08	4.4	0.086	6	29	0.9974	3.38	0.5	9	4	0
7	7.6	0.39	0.31	2.3	0.082	23	71	0.9982	3.52	0.65	9.7	5	0
8	7.9	0.43	0.21	1.6	0.106	10	37	0.9966	3.17	0.91	9.5	5	0
9	8.5	0.49	0.11	2.3	0.084	9	67	0.9968	3.17	0.53	9.4	5	0
10	6.3	0.39	0.16	1.4	0.08	11	23	0.9955	3.34	0.56	9.3	5	0
11	7.6	0.41	0.24	1.8	0.08	4	11	0.9962	3.28	0.59	9.5	5	0
12	7.9	0.43	0.21	1.6	0.106	10	37	0.9966	3.17	0.91	9.5	5	0
13	7.1	0.71	0	1.9	0.08	14	35	0.9972	3.47	0.55	9.4	5	0
14	7.8	0.645	0	2	0.082	8	16	0.9964	3.38	0.59	9.8	6	1
15	6.7	0.675	0.07	2.4	0.089	17	82	0.9958	3.35	0.54	10.1	5	0
16	6.9	0.685	0	2.5	0.105	22	37	0.9966	3.46	0.57	10.6	6	1
17	5.2	0.32	0.25	1.8	0.103	13	50	0.9957	3.38	0.55	9.2	5	0
18	7.8	0.645	0	5.5	0.086	5	18	0.9986	3.4	0.55	9.6	6	1
19	8.1	0.38	0.28	2.1	0.066	13	30	0.9968	3.23	0.73	9.7	7	1
20	5.7	1.13	0.09	1.5	0.172	7	19	0.994	3.5	0.48	9.8	4	0
')
head(mydat)

#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

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)     
fine<-rbind(fine,testList)

}

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

I attempted to get better results by reading table from csv file. Same results.

write.csv(fine, file="fine.csv",row.names=FALSE)
fineIn <- read.csv("fine.csv", header=TRUE )

```{r}
# table input as csv file is not readable, too many digits, not in scientific notation
print(fineIn)

Specifally to your examplw, you seem to be treating formatC as if it were a rare sort of function that would do inplace modification, but it is not. It is like almost all standard functions in R and will create a new output object if this is not assigned to a name, it will be effectively lost. You need to involve assignment <- to put the output somewhere.

Speaking more generally , I think you would do better to find a table formatting package and leverage that if your goal is presentation, i like gt, and flextable

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.