pivot_table: Pivot a Table

Description Usage Examples

View source: R/pivot.R

Description

Pivot a Table

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
pivot_table(
  conn,
  conn_fun,
  schema,
  table,
  id_column,
  names_from_column,
  values_from_column,
  verbose = TRUE,
  render_sql = TRUE,
  render_only = FALSE
)

Examples

 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
library(tidyverse)
conn <- pg13::local_connect()
 pg13::write_table(conn = conn,
                  schema = "public",
                  table_name = "test_table",
                  drop_existing = TRUE,
                  data =
                          tribble(~Person,~Attribute, ~Attribute_Value,
                                  "Meera", "Height", "Tall",
                                  "Syed", "Height", "Tall",
                                  "Syed", "Character", "Funny",
                                  "Meera", "Character", NA_character_))

pivot_table(conn = conn,
            schema = "public",
            table = "test_table",
            id_column = "person",
            names_from_column = "attribute",
            values_from_column = "attribute_value")


pg13::write_table(conn = conn,
                  schema = "public",
                  table_name = "test_table",
                  drop_existing = TRUE,
                  data =
                          tribble(~Person,~Attribute, ~Attribute_Value, ~Attribute_Date,
                                  "Meera", "Age", "Tall", as.Date("2020-12-27"),
                                  "Syed", "Height", "Tall", as.Date("2020-12-27"),
                                  "Syed", "Age", "Funny", as.Date("2020-12-27")))
pivot_table(conn = conn,
            schema = "public",
            table = "test_table",
            id_column = "person",
            names_from_column = "attribute",
            values_from_column = "attribute_date")


pg13::write_table(conn = conn,
                  schema = "public",
                  table_name = "test_table",
                  drop_existing = TRUE,
                  data =
                          tribble(~Person,~Measurement, ~Results, ~Results_Date,
                                  "Meera", "Age", 36, as.Date("2020-12-27"),
                                  "Syed", "Height", 74, as.Date("2020-12-27"),
                                  "Syed", "Age", 32, as.Date("2020-12-27"),
                                  "Meera", "Height", 68, as.Date("2020-12-28")
                                  )
                                )

pivot_table(conn = conn,
            schema = "public",
            table = "test_table",
            id_column = "person",
            names_from_column = "measurement",
            values_from_column = "results")


pg13::drop_table(conn = conn,
                 schema = "public",
                 table = "test_table")

pg13::dc(conn = conn)

patelm9/pg13 documentation built on Dec. 26, 2021, 8:17 p.m.