将非结构化 csv 文件转换为数据框

2023-11-30

我正在学习 R 用于文本挖掘。我有一个 CSV 格式的电视节目表。节目通常从早上 06:00 开始,一直持续到第二天凌晨 05:00,称为播出日。例如:2015 年 11 月 15 日的节目从早上 06:00 开始,到次日凌晨 05:00 结束。

以下是显示时间表的示例代码:

 read.table(textConnection("Sunday|\n 01-Nov-15|\n 6|Tom\n some information about the program|\n 23.3|Jerry\n some information about the program|\n 5|Avatar\n some information about the program|\nMonday|\n 02-Nov-15|\n 6|Tom\n some information about the program|\n 23.3|Jerry\n some information about the program|\n 5|Avatar\n some information about the program|"), header = F, sep = "|", stringsAsFactors = F)

其输出如下:

  V1|V2
Sunday |  
01-Nov-15 |       
6 | Tom  
some information about the program |       
23.3 |  Jerry  
some information about the program |       
5 | Avatar  
some information about the program |       
5.3 | Panda  
some information about the program |       
Monday  |       
02-Nov-15|       
6  Jerry  
some information about the program |      
6.25 | Panda  
some information about the program |      
23.3 | Avatar  
some information about the program |       
7.25 |   Tom  
some information about the program |      

我想将上面的数据转换成data.frame的形式

Date            |Program|Synopsis
2015-11-1 06:00 |Tom    | some information about the program
2015-11-1 23:30 |Jerry  | some information about the program
2015-11-2 05:00 |Avatar | some information about the program
2015-11-2 05:30 |Panda  | some information about the program
2015-11-2 06:00 |Jerry  | some information about the program
2015-11-2 06:25 |Panda  | some information about the program
2015-11-2 23:30 |Avatar | some information about the program
2015-11-3 07:25 |Tom    | some information about the program

我很感谢有关我应该查看的功能或包的任何建议/提示。


替代解决方案数据表:

library(data.table)
library(zoo)
library(splitstackshape)

txt <- textConnection("Sunday|\n 01-Nov-15|\n 6|Tom\n some information about the program|\n 23.3|Jerry\n some information about the program|\n 5|Avatar\n some information about the program|\nMonday|\n 02-Nov-15|\n 6|Tom\n some information about the program|\n 23.3|Jerry\n some information about the program|\n 5|Avatar\n some information about the program|")
tv <- readLines(txt)
DT <- data.table(tv)[, tv := gsub('[|]$', '', tv)]

wd <- levels(weekdays(1:7, abbreviate = FALSE))

DT <- DT[, temp := tv %chin% wd
         ][, day := tv[temp], by = 1:nrow(tvDT)
           ][, day := na.locf(day)
             ][, temp := NULL
               ][, idx := rleid(day)
                 ][, date := tv[2], by = idx
                   ][, .SD[-c(1,2)], by = idx]

DT <- cSplit(DT, sep="|", "tv", "long")[, lbl := rep(c("Time","Program","Info")), by = idx]
DT <- dcast(DT, idx + day + date + rowid(lbl) ~ lbl, value.var = "tv")[, lbl := NULL]

DT <- DT[, datetime := as.POSIXct(paste(as.character(date), sprintf("%01.2f",as.numeric(as.character(Time)))), format = "%d-%b-%y %H.%M")
   ][, datetime := datetime + (+(datetime < shift(datetime, fill=datetime[1]) & datetime < 6) * 24 * 60 * 60)
     ][, .(datetime, Program, Info)]

结果:

> DT
              datetime Program                               Info
1: 2015-11-01 06:00:00     Tom some information about the program
2: 2015-11-01 23:30:00   Jerry some information about the program
3: 2015-11-02 05:00:00  Avatar some information about the program
4: 2015-11-02 06:00:00     Tom some information about the program
5: 2015-11-02 23:30:00   Jerry some information about the program
6: 2015-11-03 05:00:00  Avatar some information about the program

解释:

1:读取数据,转换为数据表& 删除尾随|:

txt <- textConnection("Sunday|\n 01-Nov-15|\n 6|Tom\n some information about the program|\n 23.3|Jerry\n some information about the program|\n 5|Avatar\n some information about the program|\nMonday|\n 02-Nov-15|\n 6|Tom\n some information about the program|\n 23.3|Jerry\n some information about the program|\n 5|Avatar\n some information about the program|")
tv <- readLines(txt)
DT <- data.table(tv)[, tv := gsub('[|]$', '', tv)]

2:将工作日提取到新列中

wd <- levels(weekdays(1:7, abbreviate = FALSE)) # a vector with the full weekdays
DT[, temp := tv %chin% wd
   ][, day := tv[temp], by = 1:nrow(tvDT)
     ][, day := na.locf(day)
       ][, temp := NULL]

3:每天创建一个索引并创建一个包含日期的列

DT[, idx := rleid(day)][, date := tv[2], by = idx]

4:删除不必要的行

DT <- DT[, .SD[-c(1,2)], by = idx]

5:将时间和节目名称拆分为单独的行并创建标签列

DT <- cSplit(DT, sep="|", "tv", "long")[, lbl := rep(c("Time","Program","Info")), by = idx]

6:使用 data.table 开发版本中的“rowid”函数重塑为宽格式

DT <- dcast(DT, idx + day + date + rowid(idx2) ~ idx2, value.var = "tv")[, idx2 := NULL]

7:创建一个日期时间列并将深夜时间设置为第二天

DT[, datetime := as.POSIXct(paste(as.character(date), sprintf("%01.2f",as.numeric(as.character(Time)))), format = "%d-%b-%y %H.%M")
   ][, datetime := datetime + (+(datetime < shift(datetime, fill=datetime[1]) & datetime < 6) * 24 * 60 * 60)]

8:保留所需的列

DT <- DT[, .(datetime, Program, Info)]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将非结构化 csv 文件转换为数据框 的相关文章

随机推荐

  • 使用Python或graphviz绘制鱼骨图

    我正在尝试用Python生成鱼骨图 因果图或石川图 有没有办法让它更像标准的鱼骨图 from graphviz import Digraph A HW Root SW Root Electric HW ink HW windows SW D
  • Quartus Prime 中的“多个常量驱动程序”Verilog 错误

    我正在致力于用 Verilog 设计一个有限状态机来表示堆栈 该模块如下 module state machine s Enable Clock Resetn c OF Err UF Err input 2 0 s input Enable
  • 具有表达式的属性中使用的可空 DateTime 返回意外的默认值

    我有以下两个方法扩展 public static class DateTimeConverter public static DateTime Convert this DateTime time gt time null new Date
  • Java中激活其他进程的窗口

    我有两个 Java swing 应用程序 意味着在两个 JVM 中运行 有什么办法可以在它们之间切换吗 通过 Java 代码激活另一个应用程序的窗口 您可以尝试使用JNA 我将使用 Maven 为您提供一些适用于 Windows 的代码 或
  • 在 MySQL 中存储 PDF 文件

    如何将 PDF 文档存储在 MySQL 的字段中 目前 我有一个客户列表 每个客户都有一个包含其帐户信息的证书 他们可以将其提供给其他公司以证明他们是我们的客户 目前 他们的证书以 PDF 格式导出并通过电子邮件发送给工作人员 客户也会收到
  • 导航组件 popUpTo bug

    最近我遇到了我询问的问题here 但当我试图弄清楚发生了什么时 我总是偶然发现同样的问题 这就是我所拥有的 我有一个集合 预览 编辑片段 我需要在片段之间进行导航 但是从编辑片段导航到预览片段后 我希望后退按钮将用户带到集合片段而不是编辑片
  • C# - 从 FTP 下载上次修改日期较高的文件

    我有一个包含一些文件的 FTP 服务器 我在本地目录中有相同的文件 在C 当我运行该程序时 我希望它搜索 FTP 服务器中上次修改时间戳晚于本地目录中相同文件 同名 的所有文件 并下载找到的所有文件 有人可以给我帮助或提示吗 我会感谢所有的
  • 将派生类序列化/反序列化为基类

    例如我有以下课程 public abstract class Device public class WindowsDevice Device public class AndroidDevice Device 现在我想将 WindowsD
  • 如何在 .Net Core Identity 中实施 2FA?

    问题 如何强制现有用户在 Net Core 3 1 Identity 中设置 2FA 我已经在这里看到了几个答案 但我对它们有如下问题 如果用户未设置 2FA 页面 则重定向用户以在登录时设置 2FA 页面 问题是用户可以简单地跳转到不同的
  • 如何使用 Java 将字符串保存到文本文件?

    在 Java 中 我有来自名为 text 的字符串变量中的文本字段的文本 如何将 text 变量的内容保存到文件中 如果您只是输出文本 而不是任何二进制数据 则以下内容将起作用 PrintWriter out new PrintWriter
  • 有条件地单独禁用 Serilog 接收器

    我的 net core 应用程序基本配置上有 Serilog 如下所示 Log Logger new LoggerConfiguration ReadFrom Configuration Configuration Enrich FromL
  • 在 C 中将指针的地址存储在 unsigned int 中

    是否可以将指针强制转换为 unsigned int 然后将其强制转换回指针 我试图将指向结构的指针存储在 pthread t 变量中 但我似乎无法让它工作 这是我的代码的一些片段 我正在创建一个用户级线程管理库 当我尝试打印线程的 tid
  • 打开文件夹并最大化文件夹窗口

    我有以下简单的 Powershell 脚本 ii E Source Development websites example com au root ii E Source Development websites example com
  • 生成所有可能的互质的排序列表

    我需要生成所有互质的无限排序列表 每对中的第一个元素必须小于第二个元素 排序必须按升序进行 按对元素的总和 如果两个总和相等 则除以该对的第一个元素 因此 结果列表必须是 2 3 2 5 3 4 3 5 2 7 4 5 3 7 2 9 3
  • Bootstrap 5 下拉菜单向右截断

    User 的下拉列表是 cuf of 我正在使用 Bootstrap 5 我在 stackoverflow 上阅读了一篇较旧的文章 建议将 dropdown menu left dropdown menu right on the 这对我不
  • 如何使用midlrt.exe将.idl编译为.winmd?

    背景 我需要构建一个 Windows 运行时组件作为设置为使用的系统的一部分CMake生成其构建系统 作为准备步骤 我尝试在命令行上构建它 从简单的 idl 文件 MyType idl 开始 namespace NS default int
  • cookie / MAMP / CodeIgniter 的问题

    我在使用 MAMP 和 Codeigniter 读取本地主机上的 cookie 时遇到问题 我正在尝试使用 cookie 来验证对管理区域的访问 我可以设置 cookie 我在浏览器上看到它 Chrome 但在授予访问权限后我无法读取它 我
  • 接受 Java 中的证书

    我在通过 Java 与 HTTPS 站点交互时遇到问题 我的程序每次运行时都会使用一个带有不受信任证书的 URL 该程序必须在多个系统上运行 目前 我有以下内容 public class A HostnameVerifier hv new
  • Google 日历 API - 未从 Execute() C# 返回

    运行下面的代码永远不会从执行函数返回 我的个人 Gmail 帐户上有一个私人日历 已与developer gserviceaccount com 帐户共享 查看 API 管理器 用法 引用显示我已经使用过甚至点击了该 API 任何想法表示赞
  • 将非结构化 csv 文件转换为数据框

    我正在学习 R 用于文本挖掘 我有一个 CSV 格式的电视节目表 节目通常从早上 06 00 开始 一直持续到第二天凌晨 05 00 称为播出日 例如 2015 年 11 月 15 日的节目从早上 06 00 开始 到次日凌晨 05 00