根据 ocr 生成的列表在 R 中创建干净的数据框

问题描述 投票:0回答:1

我有一个表格,我使用来自 tesseract 的 ocr 函数导入到 R 中,我需要将其格式化为一个表格,该表格可用于从中创建图形表示。

我对数据进行了一系列不同的转换,到目前为止,我的最终输出需要前 3 列。

到目前为止,我的表格输出如下所示:

> table_df2
     V1 month_list us_total
1  2020   December      6.7
2  2021    January      6.3
3  2021   February      6.2
4  2021      March        6
5  2021      April      6.1
6  2021        May      5.8
7  2021       June      5.9
8  2021       July      5.4
9  2021     August      5.2
10 2021  September      4.8
11 2021    October      4.6
12 2021   November      4.2
13 2021   December      3.9

“V1”需要重命名为“Year”,“month_list”重命名为“Month”,“us_total”重命名为“U.S. Totals”

我还清除了表剩余部分中的所有值,并为这些值输出以下数据框:

> dput(values)
structure(list(table_df = c("5.6", "5.8", "14.4", "8.5", "10.5", 
"24.2", "9.1", "8.9", "16.9", "5.1", "5.5", "14.5", "8.5", "9.4", 
"17.3", "8.8", "7.7", "17.4", "5.2", "5.3", "13.1", "8.9", "10.2", 
"19.8", "8.5", "7.7", "17.3", "5", "5.2", "11.8", "8.7", "9.8", 
"18.1", "7.3", "7.5", "16.3", "4.8", "5.3", "11.1", "8.6", "10.2", 
"18.9", "7.5", "7.5", "17", "4.8", "5.1", "8.8", "8.2", "9.8", 
"12.1", "7.4", "6.7", "14.2", "5", "5.2", "9", "8.5", "10", "9.3", 
"7.9", "6.6", "13.2", "", "4.5", "4.9", "8.2", "7.6", "8.4", 
"13.3", "6.7", "6.2", "10.8", "4.3", "4.4", "9.7", "7.8", "9.0", 
"17.4", "6", "5.6", "14.9", "3.7", "4.2", "10.6", "7.2", "7.9", 
"14.6", "5.5", "5.6", "17.5", "3.8", "3.6", "10.3", "6.8", "8.2", 
"16.0", "5.6", "5", "15.6", "3.7", "3.3", "9", "4.9", "7.2", 
"22", "5.3", "4.5", "12.1", "3.1", "3.0", "8.6", "6.2", "7", 
"21.0", "4.9", "4.2", "12.2")), row.names = c(NA, -118L), class = "data.frame")

这些值需要构成我当前输出中美国总计列右侧的 9 列,其中前 9 个值是 2020 年 12 月的值,接下来的 9 个值是 2021 年 1 月的行,等等。

如果我能让它正常工作,最终输出将如下表所示(数据帧格式):

r rbind cbind
1个回答
1
投票

我能够使用矩阵函数让它工作。

my_matrix <- matrix(as.numeric(values), ncol = 9, byrow = TRUE)
values_df <- data.frame(my_matrix)
colnames(values_df) <- c("W.F", "W.M", "W.16-19", "B.F", "B.M", "B.16-19",
                         "HL.F", "HL.M", "HL.16-19")
© www.soinside.com 2019 - 2024. All rights reserved.