首页 > 解决方案 > R将数据转换为gridExtra表的科学计数法

问题描述

我在 R 中有一个名为 table 的 csv 文件,我想为我的报告制作一个漂亮的表格。为此,我只是在做:

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))

因为理想情况下这些数字非常大,所以我希望它们采用科学计数法,但是,我不知道如何做到这一点。我确实知道如何提取一列并通过这样做使其成为科学记数法

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

但我宁愿不要对每一列都这样做,然后再附加在一起,因为这很耗时。

我也试过这个:

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

但不是运气,因为我收到一个错误:

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

有什么建议吗?谢谢!!

标签: rscientific-notationgridextra

解决方案


使用dplyrscales

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

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

sapply和_sprintf

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

给你:

在此处输入图像描述


推荐阅读