0

I am trying to read an excel file in R. I used read_excel() function.

My excel file is full of numbers such as 18116.28 But R seems to recognize the numbers as date and time. R read this as 1949-08-06 06:49:24

Why does this happen? And how can I stop this?

Mir
  • 63
  • 6
  • You can specify the column types with `col_types` argument. See `help(read_excel())`. Edit: I considered you're using `readxl::read_excel()` – MonJeanJean Jul 28 '21 at 14:13
  • What package/function are you using to read in the Excel document? Have you looked at the help page for that function? Probably it has an argument about column classes... – Gregor Thomas Jul 28 '21 at 14:13
  • I am using readxl package and read_excel() function. – Mir Jul 28 '21 at 14:17
  • Thanks to your help, I tried read_excel(col_types = numeric) but it has another problem. R read numbers as numbers. But the decimal points are wrong. For example, the correct number is 18116.28 but R read it as 1.1811628e+0.4 – Mir Jul 28 '21 at 14:20
  • It's the same number, just expressed differently e.g. https://stackoverflow.com/questions/9397664/force-r-not-to-use-exponential-notation-e-g-e10 – Jonny Phelps Jul 28 '21 at 14:30
  • Thank you so much! All problems solved! – Mir Jul 28 '21 at 14:40

1 Answers1

0

You can also consider using the following approach :

library(RDCOMClient)

dir_Path <- "D:\\"
excel_File <- "test_File.xlsx"
path_Excel_File <- paste0(dir_Path, excel_File)

xlApp <- COMCreate("Excel.Application")
xlApp[["Visible"]] <- TRUE
xlWbk <- xlApp$Workbooks()$Open(path_Excel_File)
Sheets <- xlWbk$Sheets() 
nb_Sheets <- Sheets$count() 

list_DF_By_Sheet <- list()

for(l in 1 : nb_Sheets)
{
  mat <- matrix(NA, nrow = 10, ncol = 10)
  
  for(i in 1 : 10)
  {
    for(j in 1 : 10)
    {
      mat[i, j] <- Sheets[[l]]$Cells(i,j)$Text()    
    }
  }
  
  list_DF_By_Sheet[[l]] <- mat
}

With the "Sheets[[l]]$Cells(i,j)$Text()", the content of the cell with row i and column j is considered as text.

Emmanuel Hamel
  • 1,769
  • 7
  • 19