读取不以 ]dos 开头的行

标签 r

我必须导入一个包含数千行的 csv 文件。在这个文件中,标题出现了好几次。此 header 以以下四个字符开头:]dos。我想读取不包括以 ]dos 开头的行的行。

文件看起来像

N[ dos  Date dos    Heure Dos   Nom du patient  ex pat
N[ dos  Date dos    Heure Dos   Nom du patient  ex pat
7061283778  02-03-17    12h54   Mr MONTALDO JIMENA          02-03-17
7061283777  02-03-17    12h54   Mme MONTALDO JIMENA             03-03-17
7061283790  02-03-17    12h54   Mme MONTALDO JIMENA             02-03-17
7061283779  02-03-17    12h55   Mr MONTALDO JIMENA              02-03-17
7061300309  02-03-17    12h55   Mme MONTALDO JIMENA             02-03-17
7061294068  02-03-17    12h56   Mme MONTALDO JIMENA             03-03-17
7061283782  02-03-17    12h56   Mr MONTALDO JIMENA              02-03-17
N[ dos  Date dos    Heure Dos   Nom du patient  ex pat
7061283781  02-03-17    12h56   Mlle MONTALDO JIMENA            02-03-17
7061300311  02-03-17    12h57   Mme MONTALDO JIMENA             02-03-17

如您所见,标题出现了三次。


我有@Jaap 发布的方法,但我认为该文件真的很脏,在某种意义上:

  • 它是一个 csv 文件,但带有分隔符;而不是 ,(因为它是法语)。
  • 有几列有空值。
  • 也许还有未知字符(不确定)。

我收到以下错误信息:

df <- read.table(text = txt, header =FALSE) Error in scan(file = file, what = what, sep = sep, quote = quote, dec = dec, : line 1 did not have 44 elements In addition: Warning message: closing unused connection 3 (extraction.csv)

我必须用 R 编写一个程序,供对编程或数据分析一无所知的人使用。此人将定期使用该程序,以便从数据中获得一些结果。此人将无法自行清理该数据。

这是用 Excel 打开文件时的样子:

how the file looks like if you open it with Excel

最佳答案

根据示例数据,我假设您想排除以 N[ dos 开头的行。

您可以通过几个步骤在数据框中获取此数据:

  1. 使用readLines 读取数据文件。 (因为我没有文件,所以我使用了 textConnection;但您可以按如下方式使用它:readLines('name_of_your_file.txt') 读入正文)
  2. 使用 grepl 函数删除以 N[ dos 开头的行。
  3. 使用 read.table 读取剩余的文本。

完整代码:

txtcon <- textConnection('N[ dos  Date dos    Heure Dos   Nom du patient  ex pat
N[ dos  Date dos    Heure Dos   Nom du patient  ex pat
7061283778  02-03-17    12h54   Mr MONTALDO JIMENA          02-03-17
7061283777  02-03-17    12h54   Mme MONTALDO JIMENA             03-03-17
7061283790  02-03-17    12h54   Mme MONTALDO JIMENA             02-03-17
7061283779  02-03-17    12h55   Mr MONTALDO JIMENA              02-03-17
7061300309  02-03-17    12h55   Mme MONTALDO JIMENA             02-03-17
7061294068  02-03-17    12h56   Mme MONTALDO JIMENA             03-03-17
7061283782  02-03-17    12h56   Mr MONTALDO JIMENA              02-03-17
N[ dos  Date dos    Heure Dos   Nom du patient  ex pat
7061283781  02-03-17    12h56   Mlle MONTALDO JIMENA            02-03-17
7061300311  02-03-17    12h57   Mme MONTALDO JIMENA             02-03-17')

txt <- readLines(txtcon)

txt <- txt[!grepl(pattern = '^N\\[ dos', txt)]

df <- read.table(text = txt, header = FALSE)

这会产生以下数据框:

> df
          V1       V2    V3   V4       V5     V6       V7
1 7061283778 02-03-17 12h54   Mr MONTALDO JIMENA 02-03-17
2 7061283777 02-03-17 12h54  Mme MONTALDO JIMENA 03-03-17
3 7061283790 02-03-17 12h54  Mme MONTALDO JIMENA 02-03-17
4 7061283779 02-03-17 12h55   Mr MONTALDO JIMENA 02-03-17
5 7061300309 02-03-17 12h55  Mme MONTALDO JIMENA 02-03-17
6 7061294068 02-03-17 12h56  Mme MONTALDO JIMENA 03-03-17
7 7061283782 02-03-17 12h56   Mr MONTALDO JIMENA 02-03-17
8 7061283781 02-03-17 12h56 Mlle MONTALDO JIMENA 02-03-17
9 7061300311 02-03-17 12h57  Mme MONTALDO JIMENA 02-03-17

查看生成的数据框,我想您希望将一些列放在一列中。一种可能的方法是:

df2 <- data.frame(id = df$V1, 
                  datetime1 = strptime(paste(df$V2, df$V3), '%d-%m-%y %Hh%M'),
                  datetime2 = as.Date(df$V7, '%d-%m-%y'),
                  name = paste(df$V4, df$V5, df$V6))

结果是:

> df2
          id           datetime1  datetime2                 name
1 7061283778 2017-03-02 12:54:00 2017-03-02   Mr MONTALDO JIMENA
2 7061283777 2017-03-02 12:54:00 2017-03-03  Mme MONTALDO JIMENA
3 7061283790 2017-03-02 12:54:00 2017-03-02  Mme MONTALDO JIMENA
4 7061283779 2017-03-02 12:55:00 2017-03-02   Mr MONTALDO JIMENA
5 7061300309 2017-03-02 12:55:00 2017-03-02  Mme MONTALDO JIMENA
6 7061294068 2017-03-02 12:56:00 2017-03-03  Mme MONTALDO JIMENA
7 7061283782 2017-03-02 12:56:00 2017-03-02   Mr MONTALDO JIMENA
8 7061283781 2017-03-02 12:56:00 2017-03-02 Mlle MONTALDO JIMENA
9 7061300311 2017-03-02 12:57:00 2017-03-02  Mme MONTALDO JIMENA

关于读取不以 ]dos 开头的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43270789/

相关文章:

R删除pheatmap的边界

r - 带误差线的散点图

r - 自从更新 sp 包后,我通过调用 sp::CRS 定义收到警告

R options(digits=2) 函数改变总位数的格式。寻找一种更改小数点后数字的方法

R - 我应该如何格式化 B.C.E. timevis 中的日期?

r - 扩展观星者

r - 创建密度栅格并按多边形特征提取总和

r - ggplot 闪避条形图 : arrange bars by y-value per group

r - 仅设置 ggplot 限制的下限

r - 使用匹配填充数据框后的因子级别问题