r-筛选过程未获取完整数据?使用dplyr过滤器和grep



我有一个日志文件,其中一行最多有1200个字符。我想做的是先读这篇文章,然后将文件的某些部分提取到新的列中。我希望提取包含文本"[DF_API:输入字符串]"的行。当我阅读它,然后根据我感兴趣的行进行筛选时,似乎我正在丢失数据。我使用dplyr过滤器和标准grep进行了尝试,得到了相同的结果。

不知道为什么会这样。感谢你在这方面的帮助。代码和数据位于以下链接中。缎面

下方给出了代码

library(dplyr)
setwd("C:/Users/satis/Documents/VF/df_issue_dec01")
sec1 <- read.delim(file="secondary1_aa_small.log")
head(sec1)
names(sec1) <- c("V1")
sec1_test <- filter(sec1,str_detect(V1,"DF_API: input string")==TRUE)
head(sec1_test)
sec1_test2 = sec1[grep("DF_API: input string",sec1$V1, perl = TRUE),]
head(sec1_test2)
write.csv(sec1_test, file = "test_out.txt", row.names = F, quote = F)
write.csv(sec1_test2, file = "test2_out.txt", row.names = F, quote = F)

数据(和代码(在下面的链接中给出。对不起,我应该用dput。

https://spaces.hightail.com/space/arJlYkgIev

试试下面的代码,它可以为您提供一个基于匹配条件的文件中过滤行的数据帧。

#to read your file
sec1 <- readLines("secondary1_aa_small.log")
#framing a dataframe by extracting required lines from above file
new_sec1 <- data.frame(grep("DF_API: input string", sec1, value = T))
names(new_sec1) <- c("V1")

编辑:将上述列拆分为多列的简单方法

#extracting substring in between < & >
new_sec1$V1 <- gsub(".*[<t]([^>]+)[>].*", "\1", new_sec1$V1)
#replacing comma(,) with a white space
new_sec1$V1 <- gsub("[,]+", " ", new_sec1$V1)
#splitting into separate columns
new_sec1 <-  strsplit(new_sec1$V1, " ")
new_sec1 <-  lapply(new_sec1, function(x) x[x != ""] )
new_sec1 <-  do.call(rbind, new_sec1)
new_sec1 <- data.frame(new_sec1)

更改分析的列名。

最新更新