Sometimes I see data posted in a Stack Overflow question formatted like in this question. This is not the first time, so I have decided to ask a question about it, and answer the question with a way to make the posted data palatable.

我将在这里发布数据集示例,以防问题被删除.

+------------+------+------+----------+--------------------------+
|    Date    | Emp1 | Case | Priority | PriorityCountinLast7days |
+------------+------+------+----------+--------------------------+
| 2018-06-01 | A    | A1   |        0 |                        0 |
| 2018-06-03 | A    | A2   |        0 |                        1 |
| 2018-06-03 | A    | A3   |        0 |                        2 |
| 2018-06-03 | A    | A4   |        1 |                        1 |
| 2018-06-03 | A    | A5   |        2 |                        1 |
| 2018-06-04 | A    | A6   |        0 |                        3 |
| 2018-06-01 | B    | B1   |        0 |                        1 |
| 2018-06-02 | B    | B2   |        0 |                        2 |
| 2018-06-03 | B    | B3   |        0 |                        3 |
+------------+------+------+----------+--------------------------+

正如你所见,这不是发布数据的正确方式.正如a user在 comments 中所写,

按照你现在的方式格式化数据肯定花了一点时间

我相信这说明了一切.询问者的意图是好的,他们花了一些工作和时间试图表现得友好,但结果并不好.

R代码可以做些什么来使该表可用(如果有的话)?这会带来很多麻烦吗?

推荐答案

使用data.table::fread:

x = '
+------------+------+------+----------+--------------------------+
|    Date    | Emp1 | Case | Priority | PriorityCountinLast7days |
+------------+------+------+----------+--------------------------+
| 2018-06-01 | A    | A1   |        0 |                        0 |
| 2018-06-03 | A    | A2   |        0 |                        1 |
| 2018-06-03 | A    | A3   |        0 |                        2 |
| 2018-06-03 | A    | A4   |        1 |                        1 |
| 2018-06-03 | A    | A5   |        2 |                        1 |
| 2018-06-04 | A    | A6   |        0 |                        3 |
| 2018-06-01 | B    | B1   |        0 |                        1 |
| 2018-06-02 | B    | B2   |        0 |                        2 |
| 2018-06-03 | B    | B3   |        0 |                        3 |
+------------+------+------+----------+--------------------------+
'

fread(gsub('\\+.+\\n' ,'', x, perl = T), drop=c(1,7))

#          Date Emp1 Case Priority PriorityCountinLast7days
# 1: 2018-06-01    A   A1        0                        0
# 2: 2018-06-03    A   A2        0                        1
# 3: 2018-06-03    A   A3        0                        2
# 4: 2018-06-03    A   A4        1                        1
# 5: 2018-06-03    A   A5        2                        1
# 6: 2018-06-04    A   A6        0                        3
# 7: 2018-06-01    B   B1        0                        1
# 8: 2018-06-02    B   B2        0                        2
# 9: 2018-06-03    B   B3        0                        3

gsub部分删除水平规则.drop删除行尾分隔符导致的额外列.

R相关问答推荐

R图中的字体大小和字体样式(带有R底图)

如何按照特定顺序拆分字符?

如何以编程方式将X轴勾号上的希腊符号合并到R图中?

如果索引重复,聚合xts核心数据

具有多个依赖变量/LHS的逻辑模型

从嵌套列表中智能提取线性模型系数

如何将在HW上运行的R中的消息(错误、警告等)作为批处理任务输出

gt()从gt为相同内容的单元格 colored颜色 不同?

如何在R中合并和合并多个rabrame?

在R中将特定列的值向右移动

将小数分隔符放在R中的前两位数字之后

如何将R中数据帧中的任何Nas替换为最后4个值

基于R中的间隔扩展数据集行

如何将网站图像添加到带有极坐标的面包裹条形图?

如何计算R glm probit中的线性预测因子?

如何在PDF格式的kableExtra表格中显示管道字符?

R中的类别比较

在不对R中的变量分组的情况下取两行的平均值

主题(Legend.key=Element_RECT(Fill=&Quot;White&Quot;))不起作用

禁用时,SelecizeInput将变得不透明