Reading multiple Excel tables in R using readxl and the correct variable types

I have several excel files that I am trying to read in R using a package readxl. Excel files consist of several tabs, each of 60,000 rows with four variable columns. The first column is a simple integer count to track seconds from 0, 1, 2, etc. The second column is divided by a colon ( :) in HH: MM: SS. The third column is a fraction separated by date ( /) in the format MM / DD / YYYY. The fourth column is a decimal point floating point (for example, 338.6).

Using the following code, I get four columns and some formatting is consistent, but some data seems to be misinterpreted as dates or decimal numbers instead of integers, time or date.

    > data1 <- lapply(excel_sheets("./file_name.xls"),
                      read_excel, path = "./file_name.xls",
                      col_names = FALSE)
    > head(data1[[1]])
          X1       X2         X3    X4
    1 502342 02:12:50 02/04/2015 338.6
    2 502341 02:12:49 02/04/2015 338.1
    3 502340 02:12:48 02/04/2015 337.5
    4 502339 02:12:47 02/04/2015 337.6
    5 502338 02:12:46 02/04/2015 337.5
    6 502337 02:12:45 02/04/2015 338.0

    > head(data1[[2]])
            X1       X2     X3       X4
    1   483664 08:56:48 488774 08:52:22
    2 08:49:32 08:56:47 488774 08:52:22
    3    185.2 08:56:46 488774   485475
    4   483663 08:56:45 488774 08:52:22
    5 08:49:31 08:56:44 488774 08:52:22
    6   483662 08:56:43 488774   485475
    > class(data1[[2]]$X1)
    [1] "character"
    > mode(data1[[2]]$X1)
    [1] "character"

    > tail(data1[[1]])
                X1       X2     X3       X4
    59995 08:49:35 08:56:54 488774 08:52:22
    59996   483666 08:56:53 488774   485475
    59997 08:49:34 08:56:52 488774 08:51:50
    59998    185.3 08:56:51 488774 08:51:50
    59999   483665 08:56:50 488774   485475
    60000 08:49:33 08:56:49 488774   485475
    > tail(data1[[2]])
                X1       X2     X3     X4
    59995 09:29:17   497592 488774 488206
    59996   485927   497591 488774 488206
    59997 09:29:16   497590 488774 488206
    59998   485926    363.0 488774 488206
    59999 09:29:15 12:49:37 488774 488206
    60000   485925   497588 488774 488206

I am also trying to use col_typesto determine column types, but this returns a data frame full of NA.

    > data1 <- lapply(excel_sheets("./file_name.xls"),
                      read_excel, path = "./file_name.xls",
                      col_names = FALSE,
                      col_types = c("numeric", "numeric", "date","numeric"))
    There were 50 or more warnings (use warnings() to see the first 50)

    > head(data1[[1]])
      X1 X2   X3 X4
    1 NA NA <NA> NA
    2 NA NA <NA> NA
    3 NA NA <NA> NA
    4 NA NA <NA> NA
    5 NA NA <NA> NA
    6 NA NA <NA> NA

Using lapply()with read_excel()returns a list of data frames. I'm not sure if I should try to change the types of variables or how to do it exactly. Excel files themselves look consistent in terms of variable types. I even checked line 59998 in data1[[2]], which shows 363.0 for X2, but that should be 03:42:51.

excel R? . R?

.

+4

Source: https://habr.com/ru/post/1605764/


All Articles