使用可变长度将数据从文本文件(用空格分隔)中获取到R中

时间:2009-09-08 11:17:25

标签: r

我有一个与此类似的文本文件(以空格分隔):

x <- "DF12 This is an example 1 This
DF12 This is an 1232 This is
DF14 This is 12334 This is an
DF15 This 23 This is an example
"

我知道每个变量的字段长度(此数据集中有5个变量),它们是:

varlength <- c(2, 2, 18, 5, 18)

如何使用varlength变量作为字段分隔符指示符将此类数据导入R?

1 个答案:

答案 0 :(得分:2)

Per Barry Rowlingson关于r-help(你似乎已经交叉发布了):

  

?read.fwf

     

读取固定宽度格式文件

     

说明

 Read a table of *f*ixed *w*idth *f*ormatted data into a
 'data.frame'.
     

用法:

 read.fwf(file, widths, header = FALSE, sep = "\t",
          skip = 0, row.names, col.names, n = -1,
          buffersize = 2000, ...)