读取具有字符类型变量的 csv 文件,但扩展科学记数法 readr

reading in csv file with character type variable but expand scientific notation readr

我有一个带有变量 (id) 的 csv 文件。在 excel 中,当我检查单元格的格式时,有些单元格的类型是 general,有些是 scientific:

#            id
# 1 ge189839898     #general format cell in excel
# 2   we7267178     #general format cell in excel
# 3     2.8E+12     #scientific format cell in excel

当我使用 read_csv 将文件读入 R 时,它认为该列是 character(它就是我想要的),但它的意思是 2.8E+12也是一个字符。

options(digits = 22, scipen = 9999)
library(tidyverse)
dfcsv <- read_csv("file.csv")
#where dfcsv looks like:
dfcsv <- data.frame(id = c("ge189839898",
                        "we7267178",
                        "2.8E+12"))
dfcsv
#            id
# 1 ge189839898     
# 2   we7267178    
# 3     2.8E+12  

有没有一种方法可以自动读入 csv 以便正确识别混合类型的变量,因此它将是 return 一个 character 变量,但科学记数法被扩展:

#               id
# 1    ge189839898
# 2      we7267178
# 3  2800000000000

我不认为 guess_max 是我追求的 。我也不想使用 grep/sprintf 类型的解决方案(如果可能的话),因为我认为这是在尝试解决我不应该解决的问题?我偶然发现了这些有问题的 id,所以我想要一种在阅读阶段自动执行此操作的方法。

最干净的解决方案可能是进入 csv 文件并在那里进行转换,但我想通过 R.

进行转换

谢谢

id <- c("ge189839898", "we7267178", "2.8E+12")
func <- function(x) {
  poss_num <- suppressWarnings(as.numeric(x))
  isna <- is.na(poss_num)
  x[!isna] <- format(poss_num[!isna], scientific = FALSE)
  x
}
func(id)
# [1] "ge189839898"   "we7267178"     "2800000000000"