R语言 我怎样才能把这个ASCII文本文件转换成可用的数据格式呢?

ocebsuys  于 2023-02-26  发布在  其他
关注(0)|答案(3)|浏览(198)

我想使用NIBRS的“主文件下载”来进行2021年的逮捕,但是这些数据来自一个ASCII文本文件,我不知道如何转换成可用的数据集,从帮助文件中看,长数字串的某些位置对应着不同的列,但我不知道如何告诉R或Stata导入文件,而削减在这些位置。任何帮助将不胜感激!
数据的前几行如下所示:

350AK001046 921  00 20000  0000000     N0000008198                                                            KETCHIKAN                Alaska                                                                                                                                                                                                                                                                                                                                                                                                                                       
350AK001046 921  01100210               000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000001000000004000000003000000003000000001000000000000000000000000001000000001000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000001000000001000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000003000000000000000000000000008                    
350AK001046 921  01100240               000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000002000000003000000004000000000000000000000000002000000001000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000001000000001000000002000000000000000003000000000000000000000000000000000000000000000000000000000000000000000000000000000000000014000000000000000004000000000000000000000000005                    
350AK001046 921  01100260               000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000002000000006000000005000000009000000005000000005000000006000000002000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000001000000003000000002000000003000000002000000002000000001000000000000000000000000000000000000000000000000000000000000000000000000000000035000000000000000015000000000000000000000000025
ovfsdjhp

ovfsdjhp1#

假设我们在最后的注解中生成了文件lee.dat,它由8行或4行字段组成,并且我们假设8行字段将被添加到其后的4行字段之前。现在使用count.fields查找每行中的字段数,然后使用cumsum创建分组变量g,该变量将8个字段的行分组为以下4行字段。然后粘贴每个组并读取所有内容。

File <- "lee.dat"
g <- cumsum(count.fields(File) == 8)
File |>
  readLines() |>
  tapply(g, function(x) paste(x[1], x[-1])) |>
  unlist() |>
  read.table(text = _, colClasses = "character")

给予

V1  V2 V3    V4      V5          V6        V7     V8          V9 V10      V11 V12
1 350AK001046 921 00 20000 0000000 N0000008198 KETCHIKAN Alaska 350AK001046 921 01100210 000
2 350AK001046 921 00 20000 0000000 N0000008198 KETCHIKAN Alaska 350AK001046 921 01100240 000
3 350AK001046 921 00 20000 0000000 N0000008198 KETCHIKAN Alaska 350AK001046 921 01100260 000

注解

为了便于说明,我们将4个字段行中的长字段减少到3个字符。

Lines <- "350AK001046 921 00 20000 0000000 N0000008198 KETCHIKAN Alaska
350AK001046 921 01100210 000
350AK001046 921 01100240 000
350AK001046 921 01100260 000
"
cat(Lines, file = "lee.dat")
vc6uscn9

vc6uscn92#

在Stata中,这将是infix命令的作业。
如果您将示例数据放在名为test.raw的文件中,则此代码将提取前四列:

# delim ;
infix  
    ID 1-1 
    numeric_state 2-3 
    str ori_code 4-10
using test.raw;

下面是list命令的输出:

+--------------------------+
     | ID   numeri~e   ori_code |
     |--------------------------|
  1. |  3         50    AK00104 |
  2. |  3         50    AK00104 |
  3. |  3         50    AK00104 |
  4. |  3         50    AK00104 |
     +--------------------------+

我得到了最初的列标题和位置,从点击“主文件下载”下的“下载逮捕文件的帮助文件”在您提供的链接。

1cklez4t

1cklez4t3#

完成此类任务的最佳工具通常是“Excel”,但使用“大数据”时,它在第653行失败(最近通过“新冠肺炎患者损失"测试突出显示)。因此,我认为最好的方法是将文件拆分为65000行的部分,带有头部或尾部或文件拆分器,然后使用文件导入到带有列的电子表格s中。

相关问题