我想在R中编写一些长表,并希望能够控制每列的数字和格式。
先前已经讨论过控制R中的数字here,但我想要一些带有两位数的列,其他列写为e的指数。
我拥有的是:
> tab = format(round(Ind_B, 2), nsmall = 2)
logFC AveExpr t P.Value adj.P.Val B
PCL1 -1.50 7.90 -7.78 0.00 0.05 3.11
CYB5 -1.33 9.48 -7.29 0.00 0.05 2.66
YPL272C -1.14 9.84 -6.75 0.00 0.05 2.12
...
write.table(制表符,文件=" table.txt&#34)
我想要的是看似
的东西 logFC AveExpr t P.Value adj.P.Val B
PCL1 -1.50 7.90 -7.78 2.02e-05 4.66e-2 3.11
CYB5 -1.33 9.48 -7.29 3.46e-05 4.66e-2 2.66
YPL272C -1.14 9.84 -6.75 6.44e-05 4.66e-2 2.12
...
输入数据:
> dput(Ind_B[1:2,])
structure(list(logFC = c(-1.49824500263345, -1.33422303887252,
-1.13608497712886), AveExpr = c(7.89908829447163, 9.47578470147621,
9.83832131029262), t = c(-7.77827558246803, -7.29052621516572,
-6.75274781106625), P.Value = c(2.0278944567463e-05, 3.46076798843179e-05,
6.43472955950628e-05), adj.P.Val = c(0.0466525340114348, 0.0466525340114348,
0.0466525340114348), B = c(3.11252319339325, 2.65697661688921,
2.11618901483842)), .Names = c("logFC", "AveExpr", "t", "P.Value",
"adj.P.Val", "B"), row.names = c("PCL1", "CYB5", "YPL272C"), class = "data.frame")
答案 0 :(得分:4)
这是执行@Ronald建议的一个选项:指定格式化向量,然后mapply
到数据框。请注意,所有列现在都是字符。
sprintf_formats <- c(rep("%.2f", 3), rep("%.2e", 2), "%.2f")
Ind_B_sprintf <- Ind_B
Ind_B_sprintf[] <- mapply(sprintf, sprintf_formats, Ind_B)
Ind_B_sprintf
# logFC AveExpr t P.Value adj.P.Val B
#PCL1 -1.50 7.90 -7.78 2.03e-05 4.67e-02 3.11
#CYB5 -1.33 9.48 -7.29 3.46e-05 4.67e-02 2.66
#YPL272C -1.14 9.84 -6.75 6.43e-05 4.67e-02 2.12
str(Ind_B_sprintf)
#'data.frame': 3 obs. of 6 variables:
# $ logFC : chr "-1.50" "-1.33" "-1.14"
# $ AveExpr : chr "7.90" "9.48" "9.84"
# $ t : chr "-7.78" "-7.29" "-6.75"
# $ P.Value : chr "2.03e-05" "3.46e-05" "6.43e-05"
# $ adj.P.Val: chr "4.67e-02" "4.67e-02" "4.67e-02"
# $ B : chr "3.11" "2.66" "2.12"