【问题标题】:Print Data Frame columns and modified types打印数据框列和修改类型
【发布时间】:2020-04-29 22:19:41
【问题描述】:

我有用于数据科学的长宽大的表格。我经常想将数据框移动到数据库SQL 表中。这意味着我必须在create 语句中写出每个列类型。随着列数增长 > 200,执行此操作可能会很麻烦。

这似乎是一个函数的好机会。我认为它会首先检查数据框中的列类型并返回适当的Postgres 列类型,供我复制和粘贴。

R -> PG12 翻译https://www.postgresql.org/docs/12/datatype-numeric.html


| R class      | PG12 datatype             | Note                                                                                                                 |
|--------------|---------------------------|----------------------------------------------------------------------------------------------------------------------|
| factor, char | text                      | text can handle varying length strings                                                                               |
| integer      | smallint                  | if abs(x) <= 32767 then smallint                                                                                     |
| integer      | integer                   | if abs(x) <= 2147483647 then integer                                                                                 |
| integer      | bigint                    | if abs(x) <= 9223372036854775807 then bigint                                                                         |
| numeric      | smallint, integer, bigint | if there is nothing in the decimal places `4.0` coerce it to integer to save space                                   |
| numeric      | numeric(precision,scale)  | precision = nchar(unlist(strsplit(x = as.character(10.045), split = ".", fixed = T))[2]); scale = max(nchar(10.045)) |
| Date         | date                      |                                                                                                                      |

例如这张表:head(Orange)

会给出这些结果:

我可以复制并粘贴到create 语句中:

人们是否找到了任何解决方案,或者有人可以帮助解决此功能的逻辑问题?提前感谢您的帮助!

【问题讨论】:

  • @akrun 第 5 行有多种情况,因为它取决于数字列中值的最大大小。它可以是这三个中的任何一个,请参阅 PostGres 规则文档的第 2:4 行。
  • 你试过dbWriteTablefield.typesRPostgreSQL吗?

标签: python r postgresql data.table tidyverse


【解决方案1】:

还没有人回答,所以这是我研究的一个功能性(非最佳)解决方案。

x <- data.frame(smallint = 0:10, 
                smallint2 = 0.0:10.0,
                integer = 1e6:1.00001e6, 
                bigint1 = 1e11:1.0000000001e11, 
                bigint2 = 1e10:1.000000001e10, 
                bigint3 = 1e9:1.00000001e9, 
                bigint4 = 1e8:1.0000001e8, 
                text1 = LETTERS[1:11], 
                text2 = factor(LETTERS[1:11]), 
                date = as.Date("1985-01-21"),
                timestamp = Sys.time()
                , stringsAsFactors = F)

foo <- function(df){
        col.info <- sapply(X = df, FUN = function(z){RPostgreSQL::dbDataType(dbObj = con, obj = z)})
    print(data.frame(col = row.names(data.frame(col.info)), col.type = paste0(col.info,",")), row.names = F)
}
foo(df = x)
foo(df = Orange)

【讨论】:

    猜你喜欢
    • 2020-01-19
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    • 2019-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-21
    相关资源
    最近更新 更多