File: cell-and-column-types.R

package info (click to toggle)
r-cran-readxl 1.4.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,044 kB
  • sloc: ansic: 4,854; cpp: 3,213; makefile: 2
file content (72 lines) | stat: -rw-r--r-- 2,969 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
options(tibble.print_min = 4, tibble.print_max = 4)

## ----setup--------------------------------------------------------------------
library(readxl)

## ----eval = FALSE-------------------------------------------------------------
# read_excel("yo.xlsx")
# read_excel("yo.xlsx", col_types = "numeric")
# read_excel("yo.xlsx", col_types = c("date", "skip", "guess", "numeric"))

## -----------------------------------------------------------------------------
read_excel(readxl_example("deaths.xlsx"), range = cell_rows(5:15))

## -----------------------------------------------------------------------------
read_excel(
  readxl_example("deaths.xlsx"),
  range = cell_rows(5:15),
  col_types = c("guess", "skip", "guess", "skip", "skip", "skip")
)

## -----------------------------------------------------------------------------
(clippy <- 
   read_excel(readxl_example("clippy.xlsx"), col_types = c("text", "list")))
tibble::deframe(clippy)
sapply(clippy$value, class)

## -----------------------------------------------------------------------------
deaths <- read_excel(readxl_example("deaths.xlsx"))
print(deaths, n = Inf)

## -----------------------------------------------------------------------------
(nms <- names(read_excel(readxl_example("datasets.xlsx"), n_max = 0)))
(ct <- ifelse(grepl("^Petal", nms), "text", "guess"))
read_excel(readxl_example("datasets.xlsx"), col_types = ct)

## -----------------------------------------------------------------------------
df <- read_excel(readxl_example("type-me.xlsx"), sheet = "logical_coercion",
                 col_types = c("logical", "text"))
print(df, n = Inf)

## ----out.width = '70%', echo = FALSE------------------------------------------
knitr::include_graphics("img/type-me-logical.png")

## -----------------------------------------------------------------------------
df <- read_excel(readxl_example("type-me.xlsx"), sheet = "numeric_coercion",
                 col_types = c("numeric", "text"))
print(df, n = Inf)

## ----out.width = '70%', echo = FALSE------------------------------------------
knitr::include_graphics("img/type-me-numeric.png")

## -----------------------------------------------------------------------------
df <- read_excel(readxl_example("type-me.xlsx"), sheet = "date_coercion",
                 col_types = c("date", "text"))
print(df, n = Inf)

## ----out.width = '70%', echo = FALSE------------------------------------------
knitr::include_graphics("img/type-me-date.png")

## -----------------------------------------------------------------------------
df <- read_excel(readxl_example("type-me.xlsx"), sheet = "text_coercion",
                 col_types = c("text", "text"))
print(df, n = Inf)

## ----out.width = '70%', echo = FALSE------------------------------------------
knitr::include_graphics("img/type-me-text.png")