문제

I want to write some long tables in R and would like to be able to control the number of digits and formatting of each column.

Controlling digits in R have earlier been discussed here, but I want some columns with two digits, and other columns written as exponents of e.

What I have is:

> 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(tab,file="table.txt")

What I want is something that looks like

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

Input data:

> 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")
도움이 되었습니까?

해결책

Here's one option of doing what @Ronald has proposed: specify a formatting vector, and then mapply it to your data frame. Note that all columns are now characters.

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"
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top