无法使用 read_excel 在 R 中手动设置 col_types

问题描述

我正在尝试读取牛津字符串索引 covid 数据 (https://ourworldindata.org/coronavirus-source-data)。我手头的数据不是最新的,现在数据多了四列,不过我想你看一下就明白我的问题了。

我想为每一列指定数据类型,例如 col_types_owid_covid_data。然而,尽管尝试使用 col_types = col_types_owid_covid_data 做到这一点,read_excel 还是会尝试为我猜测数据类型。例如,列 hosp_patients_per_million 在它应该是数字时被转换为 logi,只是指定的。

我做错了什么?

下面的代码

    library(readxl)
    library(reshape2)
    library(ggplot2)
    library(plyr)
    library(svglite)
    library(dplyr)
    library(ggExtra)
    library(ggthemes)
    library(ggpubr)
    library(openxlsx)
    library(scales)
    library(tidyverse)
    library(Hmisc)
    library(devtools) #needed for the GitHub installation
    
    main_directory = "where I put my file"
    
    input_directory = paste0(main_directory,"Input/")
    #------------------------------------
    
    col_types_owid_covid_data <- c("text","text","date","numeric","numeric")
    
    #-----------Imports the raw data needed for the analysis----------------
    owid_covid_data <- as.data.frame(read_excel(paste0(input_directory,"owid-covid-data.xlsx"),sheet = "Data"),col_types  = col_types_owid_covid_data)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)