R 没有将 table 始终保存为整数值

R is not saving out the table consistently into integer values

出于某种原因(以前从未这样做过),R 没有以正确的方式保存文件。

无论big/small数字如何,文件都需要保存为整数。 R 正在为某些值执行此操作,但不会为其他值执行此操作。重新制作文件只是改变了合同的价值。

这是错误文件的样子:

1   834101  248830000
1   4e+06   4005000 #incorrect line
1   4955000 4965000

这是我用来获取它的代码:

write.table(outtable, 'outtable.txt', sep = "\t",
            row.names = F, col.names = F, quote = F)

这就是我需要的文件:

1   834101  248830000
1   4000000 4005000
1   4955000 4965000

如何阻止 R 将“4000000”或“6000000”写成 4e+06/6e+06?

如有任何帮助,我将不胜感激!

两个选项:

  1. options("scipen")改大一些;我相信它默认为 0,因此 此处 的 2 个或更多的东西将起作用:

    dat <- structure(list(V1 = c(1L, 1L, 1L), V2 = c(834101, 4000000, 4955000), V3 = c(248830000L, 4005000L, 4965000L)), class = "data.frame", row.names = c(NA, -3L))
    options(scipen = 2) # anything 2 or higher will work, 99 is fine too
    write.table(dat, sep = "\t", row.names = FALSE, col.names = FALSE, quote = FALSE)
    # 1 834101  248830000
    # 1 4000000 4005000
    # 1 4955000 4965000
    

    (更大的整数可能需要更高版本的 scipen=,请注意,从 ?options 开始,此数字与“宽度”的位数有关。)

  2. 写入前格式化为字符串。

    str(dat)
    # 'data.frame': 3 obs. of  3 variables:
    #  $ V1: int  1 1 1
    #  $ V2: num  834101 4000000 4955000
    #  $ V3: int  248830000 4005000 4965000
    dat[] <- lapply(dat, sprintf, fmt = "%0i")
    str(dat)
    # 'data.frame': 3 obs. of  3 variables:
    #  $ V1: chr  "1" "1" "1"
    #  $ V2: chr  "834101" "4000000" "4955000"
    #  $ V3: chr  "248830000" "4005000" "4965000"
    write.table(dat, sep = "\t", row.names = FALSE, col.names = FALSE, quote = FALSE)
    # 1 834101  248830000
    # 1 4000000 4005000
    # 1 4955000 4965000
    

    这有修改您的 table 或要求您拥有它的两个副本的副作用;如果这是一个问题,交给你。