2

fwf 读取固定宽度的文本:

lines = NULL
lines[1] = '                BUTORPHANOL TARTRATE            VIAL       2 MG/ML         '
lines[2] = '                B3/AZEL AC/ZINC/B6/COPPER/FA    TABLET     600-5-500       '

write(lines, 'lines.txt')
read.fwf('lines.txt', width = c(16, 32, 11, 12, 3), as.is = T, skip = 0)
# works:
#   V1 V2                               V3          V4           V5
# 1 NA BUTORPHANOL TARTRATE             VIAL        2 MG/ML      NA
# 2 NA B3/AZEL AC/ZINC/B6/COPPER/FA     TABLET      600-5-500    NA

添加另一行会导致错误:

lines[3] = '                C/B-6/NIACIN/FA/B12/HERB#192    CAPSULE    60-5-2.5MG      ' # this line causes error

write(lines, 'lines.txt')
read.fwf('lines.txt', width = c(16, 32, 11, 12, 3), as.is = T, skip = 0)

扫描错误(文件 = 文件,什么 = 什么,sep = sep,报价 = 报价,dec = dec,:第 3 行没有 5 个元素

我唯一能猜到的是第 3 行有一些特殊字符。有人可以帮忙吗?谢谢。

4

1 回答 1

4

我们可以指定comment.char

read.fwf('lines.txt', width = c(16, 32, 11, 12, 3), as.is = TRUE, skip = 0, comment.char="")
#  V1                               V2          V3           V4 V5
#1 NA BUTORPHANOL TARTRATE             VIAL        2 MG/ML      NA
#2 NA B3/AZEL AC/ZINC/B6/COPPER/FA     TABLET      600-5-500    NA
#3 NA C/B-6/NIACIN/FA/B12/HERB#192     CAPSULE     60-5-2.5MG   NA
于 2016-12-07T06:45:07.280 回答