首页 > 解决方案 > 将表前后元数据的.txt文件导入R

问题描述

我正在尝试将 .txt 文件导入 R,但遇到了一些困难。我的文件在文件中包含实际数据表之前和之后的元数据。我如何告诉 read.table() 函数来解决这个问题?我已经尝试过跳过参数,但没有成功。如果有人能给我指出一个好的资源的方向,那就太好了!我不能仅从文档中到达那里。

这是我的 .txt 文件的 Google Drive 链接:https ://drive.google.com/file/d/1VG3fBso0s15NGeHnN32CK68bylxrB06t/view?usp=sharing

标签: r

解决方案


data <- read.table('hackr.txt',header = T, sep =';', skip=22, stringsAsFactors = FALSE)
# Optional: cn   <- colnames(data)

tmp <- as.data.frame(stringr::str_split_fixed(data[,1], "\t",n=Inf))[2:nrow(data),]

我还可以为您清理(删除)一些垃圾列:

for (f in names(tmp)) {
  if (length(unique(tmp[[f]])) == 1) {
    cat(f, "is constant. I am deleting it.\n"); tmp[[f]] <- NULL
}}
head(tmp)
    V1           V2         V3           V4       V5       V6         V7          V8          V9         V10      V11
    2018-05-28   20:38:34   00:00:29.9   32.1     32.1     -.-        -.-         -.-         -.-                 
    2018-05-28   20:38:34   00:00:29.8   32.7     32.4     -.-        -.-         -.-         -.-                 
    2018-05-28   20:38:34   00:00:29.7   31.9     32.2     -.-        -.-         -.-         -.-                 
    2018-05-28   20:38:34   00:00:29.6   34.9     33.1     -.-        -.-         -.-         -.-                 
    2018-05-28   20:38:34   00:00:29.5   70.6     63.6     -.-        -.-         -.-         -.-                 
    2018-05-28   20:38:34   00:00:29.4   70.4     65.7     -.-        -.-         -.-         -.-

如果您想保留原始标题,只需运行可选(注释)行并使用相同类型的逻辑解析它们。它们的分隔符与表格的其余部分不同。


推荐阅读