R:使用R连接数据库处理数据

1.数据库连接

library(DBI)

library(dplyr)

library(dbplyr)

library(odbc)

con <- dbConnect(odbc::odbc(), "Oracle DB")

2.使用DBI查询数据库

dbGetQuery(con,'

select "month_idx", "year", "month",

sum(case when "term_deposit" = 'yes' then 1.0 else 0.0 end) as subscribe,

count(*) as total

from "bank"

group by "month_idx", "year", "month"

')

3.使用 dplyr 语法查询

q1 <- tbl(con, "bank") %>%
  group_by(month_idx, year, month) %>%
  summarise(
    subscribe = sum(ifelse(term_deposit == "yes", 1, 0)),
    total = n())
show_query(q1)

4.R Notebooks查询

“`{sql, connection=con, output.var = “mydataframe”}

SELECT "month_idx", "year", "month", SUM(CASE WHEN ("term_deposit" = 'yes') THEN (1.0) ELSE (0.0) END) AS "subscribe",
COUNT(*) AS "total"
FROM ("bank") 
GROUP BY "month_idx", "year", "month"
```
```{r}
library(ggplot2)
ggplot(mydataframe, aes(total, subscribe, color = year)) +
  geom_point() +
  xlab("Total contacts") +
  ylab("Term Deposit Subscriptions") +
  ggtitle("Contact volume")
```
bankggplot

5.三种查询方式比较

方法 比较
  1. DBI::dbGetQuery
  • Fewer dependencies required
  1. dplyr syntax
  • Use the same syntax for R and database objects
  • No knowledge of SQL required
  • Code is standard across SQL variants
  • Lazy evaluation
  1. R Notebook SQL engine
  • Copy and paste SQL – no formatting required
  • SQL syntax is highlighted

本站原创,如若转载,请注明出处:https://www.ouq.net/1317.html

(0)
打赏 微信打赏,为服务器增加50M流量 微信打赏,为服务器增加50M流量 支付宝打赏,为服务器增加50M流量 支付宝打赏,为服务器增加50M流量
上一篇 03/05/2022 21:08
下一篇 03/06/2022 12:57

相关推荐

  • Rstudio/Rstudio Server enable Copilot-Rstudio Server打开Copilot

    Rstudio Server 默认是关闭Copilot Copilot is turned off by default. Copilot is turned off with copilot-enabled=0 in /etc/rstud…

    R 06/05/2025
    111
  • R_Code: KEGG analysis

    library(clusterProfiler) library(org.Hs.eg.db) # 读取输入数据文件 file_path <- “C:/Users/Lamarck/Desktop/UP_genes_ENSEMBL_ENT…

    R 06/02/2025
    134
  • R_Code:GO and Functional GO

    GO analysis: library(AnnotationDbi) library(org.Hs.eg.db) #基因注释包 library(clusterProfiler) #富集包 # 读取CSV文件 file_path <-…

    R 06/02/2025
    136
  • R_Code:WGCNA and WGCNA_Get_Gene_Length

    library(WGCNA) library(DESeq2) # enableWGCNAThreads(nThreads = 10) # 在处理数据框(data.frame)时,不会自动给将String类型转换成factor类型 optio…

    R 06/02/2025
    139
  • R_Code:RNAseq_GSEA_analysis

    library(clusterProfiler) # GSEA 和富集分析主力包 library(org.Hs.eg.db) # 人类注释数据库(ENTREZID 与 SYMBOL 等 ID 转换) library(enrichplot) …

    R 06/02/2025
    137