read.delim没有给我我想要的 [英] read.delim not giving me what i want

查看:80
本文介绍了read.delim没有给我我想要的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是R的新手,我正在做一个项目,我的代码需要帮助。我尝试使用不同的读取功能读取数据,但没有给我我想要的。我希望能够定义有关如何正确读取文件的格式。

Hi i am new to R and i am working on a project i need help on my code. i tried reading the data using the different read functions but its not giving me what i want. I want to be able to define the format on how to read the file correctly.

这里是一个示例,显示我的数据在Txt文件中的样子。

Here is an example of how my data looks like in the Txt file. it is separated by comma and semicolon so its difficult to read.

08.08.2019 23:44:25,036 : FB_Packet detection: no pack regognised, Graber is not free
08.08.2019 23:43:40,087 : FB_Packet detection: Packet with axis, width: 95.6640014648438

我想使用

08.08.2019, 23:43:40,087,  FB_Packet detection: no pack recognised,  Graber is not free

预先感谢您

推荐答案

最初,我在考虑使用正则表达式提取所需片段的方法;根据格式是否变得更加复杂,这可能是最佳选择。否则,您可以尝试以下操作: tidyr :: separate 将文本分成几列,每隔一个分隔符一次( )。

Originally I was thinking of ways to extract the pieces you want with regex; depending on whether the formatting becomes any more complex, that might be the best option. Otherwise, you could try this with a few passes of tidyr::separate to split the text into columns, once for each different delimeter (" : ", " ", and ", ").

与其读入文件以准备使用 read.delim 或类似的东西进行解析,不如读取文本行并自行拆分。然后在每个分度计上分开;由于这会删除要分隔的列,因此您无需在提取完文本后删除任何文本。

Instead of reading in the file as something ready to parse with read.delim or something similar, just read the lines of text and split them yourself. Then separate on each of those delimeters; since this drops the column being separated, you don't have to delete any text after you're done extracting it.

library(tidyr)

txt <- readr::read_lines("08.08.2019 23:44:25,036 : FB_Packet detection: no pack regognised, Graber is not free
08.08.2019 23:43:40,087 : FB_Packet detection: Packet with axis, width: 95.6640014648438")

data.frame(txt) %>%
  separate(txt, into = c("datetime", "string"), sep = " : ") %>%
  separate(datetime, into = c("date", "time"), sep = " ") %>%
  separate(string, into = c("txt1", "txt2"), sep = ", ")
#>         date         time                                    txt1
#> 1 08.08.2019 23:44:25,036 FB_Packet detection: no pack regognised
#> 2 08.08.2019 23:43:40,087   FB_Packet detection: Packet with axis
#>                      txt2
#> 1      Graber is not free
#> 2 width: 95.6640014648438

这篇关于read.delim没有给我我想要的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆