r - 在R中将行组合到数据框
问题描述
我有一些这种格式的数据,我想在 R 中导入,到目前为止,我使用 read.csv 来读取它们,但每一块都以自己的行结束。
18.07.19
05:41:05
Information
18.07.19
05:43:48
Something
18.07.19
05:20:48
Text
18.07.19
01:16:45
到目前为止很好,但我需要它是这种格式:
18.07.19 05:41:05 Information
18.07.19 05:43:48 Something
18.07.19 05:20:48 Text
18.07.19 01:16:45
因为我想将数据用作数据框。
我认为 dcast 可能是正确的方法,但我无法弄清楚我必须作为参数传递什么。
解决方案
这是一个黑客,data.table::dcast
因为你提到过:
x <- read.csv(header=FALSE, stringsAsFactors=FALSE, text="
18.07.19
05:41:05
Information
18.07.19
05:43:48
Something
18.07.19
05:20:48
Text
18.07.19
01:16:45")
x$i <- head(rep(1:3, times=ceiling(nrow(x) / 3)), n = nrow(x))
x$j <- head(rep(1:ceiling(nrow(x)), each=3), n = nrow(x))
data.table::dcast(x, j ~ i, value.var="V1")
# j 1 2 3
# 1 1 18.07.19 05:41:05 Information
# 2 2 18.07.19 05:43:48 Something
# 3 3 18.07.19 05:20:48 Text
# 4 4 18.07.19 01:16:45 <NA>
(您可以轻松删除j
和重命名列名。)
推荐阅读
- javascript - 如何将switch语句中的模板字符串与js匹配?
- regex - 需要将3个公式组合成1个
- angularjs - 如何检查所有和取消选中Angularjs中的所有静态复选框?
- azure - 使用 Terraform 的 Azure 数据工厂诊断设置的资源特定目标表
- if-statement - 我可以将 IF 函数的值转换为方程式吗?
- go - 如何自动使测试在包内并行运行?
- python-3.x - 如何将具有相同标题的数据移动到同一列中?
- php - 网站的 PHP 文件显示错误:找不到驱动程序
- android - 如何在抽屉布局下方放置广告?
- java - 每当 App 在 android pie 中被杀死时,服务也会被杀死