Amaranta_Remedios
Amaranta_Remedios

Reputation: 793

R convert data to scientific notation for gridExtra table

I have a csv file in R called table that I want to make into a nice table for my report. To do so I am simply doing:

table <- structure(list(Total_reads = c(13849346L, 23529154L, 31998434L, 
27254596L, 26701166L, 161392392L, 106210430L, 51113256L, 13779722L, 
6815520L, 6353128L), Mapped_genome = c(12702092L, 21557650L, 
29016304L, 24850740L, 25815390L, 160743994L, 105691988L, 50607098L, 
13251934L, 6218818L, 5859958L), Mapped_tRNAs.rRNAs = c(9763646L, 
17523976L, 25025358L, 18200894L, 8294856L, 4163306L, 3246208L, 
3042706L, 2903340L, 2387962L, 2493014L), Mapped_microRNAs = c(9164514L, 
17191466L, 25009496L, 18198362L, 8292590L, 4161890L, 3245200L, 
3041844L, 2901536L, 2387174L, 2492702L), Mapped_TE.s = c(2587558L, 
5154946L, 7845327L, 5566188L, 2362836L, 1115348L, 857757L, 802486L, 
638315L, 512231L, 593754L), Mapped_TE.s.10A.1U. = c(920759L, 
2321612L, 3755844L, 2663580L, 1005029L, 340408L, 199877L, 245643L, 
131004L, 135578L, 125950L), Mapped_piRNAs_clusters = c(233231L, 
683284L, 1279722L, 948552L, 288762L, 42358L, 6611L, 2067L, 968L, 
546L, 561L)), class = "data.frame", row.names = 25:35)

grid.table(format(table,scientific=TRUE))

Because the numbers are quite big ideally I want them in scientific notation, however, I don't know how to do this. I do know how to extract a column and make it scientific notation by doing

c1<- formatC(table[,1],format="e")

But I rather not do that to every column to then append together because is time consuming.

I have also tried this:

c1<- formatC(table[,c(1,2,3,4,5,6,7)],format="e")

But not luck because I get an error:

Error in is.finite(x) : default method not implemented for type 'list'
In addition: Warning message:
In formatC(table[, c(1, 2, 3, 4, 5, 6)], format = "e") :
  class of 'x' was discarded

Any suggestion? Thanks!!

Upvotes: 0

Views: 365

Answers (1)

Peter
Peter

Reputation: 12729

Using dplyr and scales

library(gridExtra)
library(dplyr)
library(scales)

table1 <- mutate_all(table, scientific)
             
grid.table(table1)

With sapply and sprintf

grid.table(sapply(table, function(x) sprintf("%.2e", x)))

gives you:

enter image description here

Upvotes: 2

Related Questions