【问题标题】:Importing large numbers when binding csv files绑定csv文件时导入大数
【发布时间】:2017-01-12 08:42:48
【问题描述】:

尝试导入包含超过最大整数 (.Machine$integer.max = 2147483647) 的列的数据时,我的代码出现问题。使用 readr 的 read_csv 我相信它是作为 NA 导入的,而不是四舍五入。复杂性来自尝试使用 rbindlist 导入多个 csv。

这是我目前的设置:

 load_df_path <- file.path(".../dffolder") #path to folder
 df_path_files <- list.files <- (load_df_path, full.names = TRUE) #list files in path

 df <- rbindlist(lapply(df_path_files, read_csv)) # read in csvs using readr

如何编写最后一行以导入 csv 并将“数量”列转换为字符而不是整数?

这是我尝试过的一些事情,但没有运气......

## This gets error: Error in switch(tools::file_ext(path)....
 df <- rbindlist(lapply(df_path_files, read_csv(df_path_files, col_types = list(amount = col_character())))) 


## recreate read_csv and changed col_types = NULL to the above but getting the warning
## Error in FUN(X[[i]], ...) : could not find function "read_delimited"

tl;dr - 在将特定列更改为字符格式或 int64 时需要帮助导入 csv 列表。

谢谢。

【问题讨论】:

    标签: r readr


    【解决方案1】:

    你快到了,只是语法......

    df_list <- lapply(df_path_files, read_csv, col_types = cols(amount = col_character()))
    df <- rbindlist(df_list)
    

    col_types 期望 NULL 或由 cols 创建的东西。请参阅?read_csv?cols

    另一个想法:也许强制执行numeric 而不是int 可能是一个解决方案:使用cols(amount = col_double()) 看这里: long/bigint/decimal equivalent datatype in R

    【讨论】:

    • 感谢您的语法修复!我会看看使用数字!
    猜你喜欢
    • 2012-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-15
    • 1970-01-01
    相关资源
    最近更新 更多