fread 和 grepl 一起

fread together with grepl

我有一个数据(大数据125000行,~20MB),在读取过程中需要删除一些带有特定字符串的行,并需要选择一些列。

首先,我发现 grepl 函数无法正常工作,因为 fread 将数据作为一列显示在 中。

可以找到示例数据 here(通过遵循@akrun 建议)和 header 这样的数据

头(sum_data)

TRIAL :            1        3331        9091
  TRIAL :            2  1384786531   278055555
    2     0.10     0.000E+00 -0.0047 -0.0168 -0.9938    -0.0087 -0.0105 -0.9709     0.0035  0.0079 -0.9754     0.0081  0.0023  0.9997      -0.135324E-09    0.278754E-01
    2     0.20     0.000E+00 -0.0121  0.0002 -0.9898    -0.0364 -0.0027 -0.9925    -0.0242 -0.0050 -0.9929     0.0029 -0.0023  0.9998      -0.133521E-09    0.425567E-01
    2     0.30     0.000E+00  0.0193 -0.0068 -0.9884     0.0040  0.0139 -0.9782    -0.0158  0.0150 -0.9814     0.0054 -0.0008  0.9997      -0.134103E-09    0.255356E-01
    2     0.40     0.000E+00 -0.0157  0.0183 -0.9879    -0.0315 -0.0311 -0.9908    -0.0314 -0.0160 -0.9929     0.0040  0.0010  0.9998      -0.134819E-09    0.257300E-01
    2     0.50     0.000E+00 -0.0402  0.0300 -0.9832    -0.0093  0.0269 -0.9781    -0.0326  0.0247 -0.9802     0.0044 -0.0010  0.9997      -0.131515E-09    0.440350E-01

我尝试使用 fread 读取数据并使用 grepl 删除行;

files <-dir(pattern = "*sum.txt",full.names = FALSE)
library(data.table)

fread_files <- function(files){
sum_data_read <- fread(files,skip=2, sep="\t", ) #seperation is tab.
df_grep <- sum_vgm_read [!grepl("TRI",sum_vgm_read$V1),] # for removing the lines that contain "TRIAL" letter in V1 column. But so far there is no V1 column is recognized!!

df <- bind_rows(df_grep)  #binding rows after removing 
write.table(as.data.table(df),file = gsub("(.*)(\..*)", "\1_new\2", files),row.names = FALSE,col.names = TRUE) 
}

最后 lapply

lapply(files, fread_files)

当我执行此操作时,只有一行数据被创建为输出,这是正在发生的事情,但我不知道是什么。 提前感谢您的帮助!

为了读取文件并根据字符串条件删除行,您可以使用 readLines 函数,并过滤结果。

我使用 stringr 包进行字符串操作。

library(stringr)
# Read your file by lines
DT <- readLines("sum_data") 
length(DT)
#> [1] 124501
# detect which lines contains trial
trial_lines <- str_detect(DT, "TRI")
head(trial_lines)
#> [1]  TRUE  TRUE FALSE FALSE FALSE FALSE
# Remove those lines 
DT <- DT[!trial_lines]
length(DT)
#> [1] 124251
# Rewrite your file by line
writeLines(DT, "new_file")

如果您遇到性能问题,您可以尝试使用包 readr 中的 read_lines 而不是基础 readLines

Firstly, I discovered that grepl function does not work properly since fread makes the data as one column indicated also in .

但是该问题的已接受答案表明该问题已在 v1.9.6 中修复。您使用的是哪个版本?这就是为什么我们要求您预先说明版本号,以节省回答时间。

这是一个很好的示例文件,问题也很好。

我不会尝试重新发明轮子,因为这些操作早已作为命令行工具实现,您可以直接与 fread 一起使用。优点是您不会搅动 R 内存,您可以将过滤留给命令工具,这样效率更高。例如,如果您将所有行作为行加载到 R 中,这些字符串将缓存在 R 的全局字符串缓存中(至少是暂时的)。首先在 R 之外执行该过滤器将节省该成本。

我下载了你的好文件并测试了以下文件是否有效。

> fread("grep -v TRIAL sum_data.txt")
         V1   V2 V3      V4      V5      V6      V7      V8      V9     V10     V11     V12    V13     V14    V15          V16       V17
     1:   2  0.1  0 -0.0047 -0.0168 -0.9938 -0.0087 -0.0105 -0.9709  0.0035  0.0079 -0.9754 0.0081  0.0023 0.9997 -1.35324e-10 0.0278754
     2:   2  0.2  0 -0.0121  0.0002 -0.9898 -0.0364 -0.0027 -0.9925 -0.0242 -0.0050 -0.9929 0.0029 -0.0023 0.9998 -1.33521e-10 0.0425567
     3:   2  0.3  0  0.0193 -0.0068 -0.9884  0.0040  0.0139 -0.9782 -0.0158  0.0150 -0.9814 0.0054 -0.0008 0.9997 -1.34103e-10 0.0255356
     4:   2  0.4  0 -0.0157  0.0183 -0.9879 -0.0315 -0.0311 -0.9908 -0.0314 -0.0160 -0.9929 0.0040  0.0010 0.9998 -1.34819e-10 0.0257300
     5:   2  0.5  0 -0.0402  0.0300 -0.9832 -0.0093  0.0269 -0.9781 -0.0326  0.0247 -0.9802 0.0044 -0.0010 0.9997 -1.31515e-10 0.0440350
    ---                                                      

124247: 250 49.5  0 -0.0040  0.0141  0.9802 -0.0152  0.0203 -0.9877 -0.0015  0.0123 -0.9901 0.0069  0.0003 0.9997 -1.30220e-10 0.0213215
124248: 250 49.6  0 -0.0006  0.0284  0.9819  0.0021  0.0248 -0.9920  0.0264  0.0408 -0.9919 0.0028 -0.0028 0.9997 -1.30295e-10 0.0284142
124249: 250 49.7  0  0.0378  0.0305  0.9779 -0.0261  0.0232 -0.9897 -0.0236  0.0137 -0.9928 0.0102 -0.0023 0.9997 -1.29890e-10 0.0410760
124250: 250 49.8  0  0.0569 -0.0203  0.9800 -0.0028 -0.0009 -0.9906 -0.0139 -0.0169 -0.9918 0.0039 -0.0017 0.9997 -1.31555e-10 0.0513482
124251: 250 49.9  0  0.0234 -0.0358  0.9840 -0.0340  0.0114 -0.9873 -0.0255  0.0134 -0.9888 0.0006  0.0009 0.9997 -1.30862e-10 0.0334976
>

-v 使 grep return 所有行 除了 包含字符串 TRIAL 的行。考虑到多年来查看命令工具 grep 的高质量工程师的数量,它很可能是您能得到的最快的,并且正确、方便、在线文档齐全、容易学习和寻找特定任务的解决方案。如果您需要做更复杂的字符串过滤器(例如行首或行尾的字符串等),那么 grep 语法非常强大。学习它的语法是一种可转移到其他语言和环境的技能。

有关在fread中使用命令行工具的更多示例,您可以查看文章Convenience features of fread. Please note that "On Windows we recommend Cygwin(运行 需要安装一个.exe),其中包括命令行工具例如 grep".