這將使用來自 Lahman 棒球數據源的數據創建一個有趣的數據庫,該數據源由 Sean Lahman 提供,位於http://seanlahman.com/download-baseball-database/,並且可以通過以下方式輕鬆地在 R 中使用拉赫曼邁克爾·弗蘭德利、丹尼斯·墨菲和馬丁·蒙克曼的作品集。有關各個表的文檔,請參閱該包的文檔。
用法
lahman_sqlite(path = NULL)
lahman_postgres(dbname = "lahman", host = "localhost", ...)
lahman_mysql(dbname = "lahman", ...)
copy_lahman(con, ...)
has_lahman(type, ...)
lahman_srcs(..., quiet = NULL)
參數
- ...
-
其他參數在首次加載時傳遞給
src
。對於 MySQL 和 PostgreSQL,默認情況下假設您有一個本地服務器,並且已經創建了lahman
數據庫。對於lahman_srcs()
,提供要生成的 src 的名稱的字符向量。 - type
-
源代碼類型。
- quiet
-
如果是
TRUE
,則禁止顯示有關數據庫連接失敗的消息。
例子
# Connect to a local sqlite database, if already created
# \donttest{
library(dplyr)
if (has_lahman("sqlite")) {
lahman_sqlite()
batting <- tbl(lahman_sqlite(), "Batting")
batting
}
#> Creating table: AllstarFull
#> Creating table: Appearances
#> Creating table: AwardsManagers
#> Creating table: AwardsPlayers
#> Creating table: AwardsShareManagers
#> Creating table: AwardsSharePlayers
#> Creating table: Batting
#> Creating table: BattingPost
#> Creating table: CollegePlaying
#> Creating table: Fielding
#> Creating table: FieldingOF
#> Creating table: FieldingOFsplit
#> Creating table: FieldingPost
#> Creating table: HallOfFame
#> Creating table: HomeGames
#> Creating table: LahmanData
#> Creating table: Managers
#> Creating table: ManagersHalf
#> Creating table: Parks
#> Creating table: People
#> Creating table: Pitching
#> Creating table: PitchingPost
#> Creating table: Salaries
#> Creating table: Schools
#> Creating table: SeriesPost
#> Creating table: Teams
#> Creating table: TeamsFranchises
#> Creating table: TeamsHalf
#> # Source: table<Batting> [?? x 22]
#> # Database: sqlite 3.41.2 [/tmp/Rtmp5JwGfx/lahman.sqlite]
#> playerID yearID stint teamID lgID G AB R H X2B X3B
#> <chr> <int> <int> <chr> <chr> <int> <int> <int> <int> <int> <int>
#> 1 abercda01 1871 1 TRO NA 1 4 0 0 0 0
#> 2 addybo01 1871 1 RC1 NA 25 118 30 32 6 0
#> 3 allisar01 1871 1 CL1 NA 29 137 28 40 4 5
#> 4 allisdo01 1871 1 WS3 NA 27 133 28 44 10 2
#> 5 ansonca01 1871 1 RC1 NA 25 120 29 39 11 3
#> 6 armstbo01 1871 1 FW1 NA 12 49 9 11 2 1
#> 7 barkeal01 1871 1 RC1 NA 1 4 0 1 0 0
#> 8 barnero01 1871 1 BS1 NA 31 157 66 63 10 9
#> 9 barrebi01 1871 1 FW1 NA 1 5 1 1 1 0
#> 10 barrofr01 1871 1 BS1 NA 18 86 13 13 2 1
#> # ℹ more rows
#> # ℹ 11 more variables: HR <int>, RBI <int>, SB <int>, CS <int>, BB <int>,
#> # SO <int>, IBB <int>, HBP <int>, SH <int>, SF <int>, GIDP <int>
# Connect to a local postgres database with lahman database, if available
if (has_lahman("postgres")) {
lahman_postgres()
batting <- tbl(lahman_postgres(), "Batting")
}
#> Error: connection to server at "localhost" (::1), port 5432 failed: Connection refused
#> Is the server running on that host and accepting TCP/IP connections?
#> connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused
#> Is the server running on that host and accepting TCP/IP connections?
# }
相關用法
- R dbplyr backend-teradata 後端:Teradata
- R dbplyr escape 轉義/引用字符串。
- R dbplyr expand.tbl_lazy 擴展 SQL 表以包含所有可能的值組合
- R dbplyr distinct.tbl_lazy 子集不同/唯一行
- R dbplyr backend-sqlite 後端:SQLite
- R dbplyr pivot_wider.tbl_lazy 將數據從長軸轉向寬軸
- R dbplyr build_sql 構建 SQL 字符串。
- R dbplyr mutate.tbl_lazy 創建、修改和刪除列
- R dbplyr collapse.tbl_sql 計算查詢的結果
- R dbplyr sql_expr 從 R 表達式生成 SQL
- R dbplyr get_returned_rows 提取並檢查返回的行
- R dbplyr dbplyr_uncount “計數”數據庫表
- R dbplyr count.tbl_lazy 按組計數觀察值
- R dbplyr backend-odbc 後端:ODBC
- R dbplyr head.tbl_lazy 對第一行進行子集化
- R dbplyr db-quote SQL 轉義/引用泛型
- R dbplyr copy_inline 在 dbplyr 查詢中使用本地 DataFrame
- R dbplyr backend-oracle 後端:甲骨文
- R dbplyr backend-snowflake 後端:雪花
- R dbplyr backend-redshift 後端:紅移
- R dbplyr partial_eval 部分評估表達式。
- R dbplyr group_by.tbl_lazy 按一個或多個變量分組
- R dbplyr tbl_lazy 創建本地惰性 tibble
- R dbplyr backend-hana 後端:SAP HANA
- R dbplyr translate_sql 將表達式轉換為 SQL
注:本文由純淨天空篩選整理自Hadley Wickham等大神的英文原創作品 Cache and retrieve an src_sqlite of the Lahman baseball database.。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。