繁体   English   中英

R write.table to txt with specified leading 0s

[英]R write.table to txt with specified leading 0s

我有一个 df 需要导出为 txt。 但是我想让一些变量考虑它们的宽度,所以在下面的简化示例中(实际 df 有大约 300 列):

col1 <-  c(1,2,3,4,5)
col2 <-  c(1,6,7,10,1)

df <- data.frame(col1,col2)
write.table(df, file = "dataset.txt", sep = "", row.names = F, col.names = F)

我得到的是:

11
26
37
410
51

但我需要的是

101
206
307
410
501

因此,由于某些变量的宽度可以为 2,因此我需要在其他语言(例如 SPSS)中添加前导零,您可以这样做:

WRITE OUTFILE=dataset.txt /
col1                (N1)
col1                (N2).

R有这样的东西吗? 谢谢!

一种简单的方法是在导出数据集之前简单地添加前导零:

df$col2 <- sprintf("%02d", df$col2)

这里有一个解决方案,将lapply()gdata::write.fwf()结合起来写入一个固定的记录文件。

我们将使用mtcars数据,将行名称转换为数据框中的列,使用sprintf()格式化列,并将它们写入 output 文件,该文件可以使用base::read.fwf()或其他程序读取读取固定记录文件。

data <- cbind(car = rownames(mtcars),mtcars)
fmtList <- c("%20s","%03.1f","%02d","%05.1f","%04d","%04.2f",
             "%06.3f","%05.2f","%02d","%02d","%02d","%02d")
result <- lapply(1:12,function(x,y,z) {
     y[[x]] <- sprintf(z[x],y[[x]])
},data,fmtList)

output <- do.call(cbind,result)
library(gdata)
write.fwf(output,'./data/output.dat',
          rownames = FALSE,colnames = FALSE,
          formatInfo = TRUE)

write.fwf()生成一个格式列表,列出 output 文件中每个变量的开始和结束列。

> write.fwf(output,'./data/output.dat',
+           rownames = FALSE,colnames = FALSE,
+           formatInfo = TRUE)
   colname nlevels position width digits exp
1       V1      32        1    20      0   0
2       V2      25       22     4      0   0
3       V3       3       27     2      0   0
4       V4      27       30     5      0   0
5       V5      22       36     4      0   0
6       V6      22       41     4      0   0
7       V7      29       46     6      0   0
8       V8      30       53     5      0   0
9       V9       2       59     2      0   0
10     V10       2       62     2      0   0
11     V11       3       65     2      0   0
12     V12       6       68     2      0   0
>

...以及生成的 output 文件的前几行:

           Mazda RX4 21.0 06 160.0 0110 3.90 02.620 16.46 00 01 04 04
       Mazda RX4 Wag 21.0 06 160.0 0110 3.90 02.875 17.02 00 01 04 04
          Datsun 710 22.8 04 108.0 0093 3.85 02.320 18.61 01 01 04 01
      Hornet 4 Drive 21.4 06 258.0 0110 3.08 03.215 19.44 01 00 03 01
   Hornet Sportabout 18.7 08 360.0 0175 3.15 03.440 17.02 00 00 03 02
             Valiant 18.1 06 225.0 0105 2.76 03.460 20.22 01 00 03 01
          Duster 360 14.3 08 360.0 0245 3.21 03.570 15.84 00 00 03 04
           Merc 240D 24.4 04 146.7 0062 3.69 03.190 20.00 01 00 04 02
            Merc 230 22.8 04 140.8 0095 3.92 03.150 22.90 01 00 04 02
            Merc 280 19.2 06 167.6 0123 3.92 03.440 18.30 01 00 04 04

为了消除分隔列的空格,我们可以将sep = ""添加到write.fwf() function。

write.fwf(output,'./data/output.dat',
          rownames = FALSE,colnames = FALSE,
          formatInfo = TRUE,sep = "")

...以及修改后的 output 文件的前 10 行:

           Mazda RX421.006160.001103.9002.62016.4600010404
       Mazda RX4 Wag21.006160.001103.9002.87517.0200010404
          Datsun 71022.804108.000933.8502.32018.6101010401
      Hornet 4 Drive21.406258.001103.0803.21519.4401000301
   Hornet Sportabout18.708360.001753.1503.44017.0200000302
             Valiant18.106225.001052.7603.46020.2201000301
          Duster 36014.308360.002453.2103.57015.8400000304
           Merc 240D24.404146.700623.6903.19020.0001000402
            Merc 23022.804140.800953.9203.15022.9001000402
            Merc 28019.206167.601233.9203.44018.3001000404

您可以使用str_pad中的stringr

library(stringr)  
df$col3 <- str_c(df$col1, str_pad(df$col2, 2, pad = 0))
df

#  col1 col2 col3
#1    1    1  101
#2    2    6  206
#3    3    7  307
#4    4   10  410
#5    5    1  501

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM