咱們也將展現怎麼利用R語言的函數來實現這些功能。近期我在處理一些FDA(譯者注:食品及藥物管理局)的不良事件數據。這些數據很混亂:有缺失值。有反覆記錄,有不一樣一時候間創建的數據集的可比性問題。不一樣數據集中變量名稱和數量也不統一(比方一個數據集裏叫sex,還有一個裏叫gender),還有疏忽錯誤等問題。但正因如此,這些數據對於數據科學家或者愛好者而言到是理想的練手對象。php
require(downloader) library(dplyr) library(sqldf) library(data.table) library(ggplot2) library(compare) library(plotrix)
現在讓咱們下載數據的壓縮包並將其解壓。react
try.error = function(url)
{
try_error = tryCatch(download(url,dest="data.zip"), error=function(e) e)
if (!inherits(try_error, "error")){
download(url,dest="data.zip")
unzip ("data.zip")
}
else if (inherits(try_error, "error")){
cat(url,"not found\n")
}
}
year_start=2013
year_last=year(Sys.time())
for (i in year_start:year_last){
j=c(1:4)
for (m in j){
url1<-paste0("http://www.nber.org/fda/faers/",i,"/demo",i,"q",m,".csv.zip")
url2<-paste0("http://www.nber.org/fda/faers/",i,"/drug",i,"q",m,".csv.zip")
url3<-paste0("http://www.nber.org/fda/faers/",i,"/reac",i,"q",m,".csv.zip")
url4<-paste0("http://www.nber.org/fda/faers/",i,"/outc",i,"q",m,".csv.zip")
url5<-paste0("http://www.nber.org/fda/faers/",i,"/indi",i,"q",m,".csv.zip")
try.error(url1)
try.error(url2)
try.error(url3)
try.error(url4)
try.error(url5)
}
}
http://www.nber.org/fda/faers/2015/demo2015q4.csv.zip not found
...
http://www.nber.org/fda/faers/2016/indi2016q4.csv.zip not found
比方^demo.*.csv表示所有名字以demo開頭的csv文件。web
filenames <- list.files(pattern="^demo.*.csv", full.names=TRUE)
cat('We have downloaded the following quarterly demography datasets')
filenames
"./demo2012q1.csv" "./demo2012q2.csv" "./demo2012q3.csv" "./demo2012q4.csv" "./demo2013q1.csv" "./demo2013q2.csv" "./demo2013q3.csv" "./demo2013q4.csv" "./demo2014q1.csv" "./demo2014q2.csv" "./demo2014q3.csv" "./demo2014q4.csv" "./demo2015q1.csv" "./demo2015q2.csv" "./demo2015q3.csv"
demo=lapply(filenames,fread)
demo_all=do.call(rbind,lapply(1:length(demo),function(i) select(as.data.frame(demo[i]),primaryid,caseid, age,age_cod,event_dt,sex,reporter_country))) dim(demo_all) 3554979 7
filenames <- list.files(pattern="^drug.*.csv", full.names=TRUE)
cat('We have downloaded the following quarterly drug datasets:\n')
filenames
drug=lapply(filenames,fread)
cat('\n')
cat('Variable names:\n')
names(drug[[1]])
drug_all=do.call(rbind,lapply(1:length(drug), function(i) select(as.data.frame(drug[i]),primaryid,caseid, drug_seq,drugname,route)))
"./drug2012q1.csv" "./drug2012q2.csv" "./drug2012q3.csv" "./drug2012q4.csv" "./drug2013q1.csv" "./drug2013q2.csv" "./drug2013q3.csv" "./drug2013q4.csv" "./drug2014q1.csv" "./drug2014q2.csv" "./drug2014q3.csv" "./drug2014q4.csv" "./drug2015q1.csv" "./drug2015q2.csv" "./drug2015q3.csv"
"primaryid" "drug_seq" "role_cod" "drugname" "val_vbm" "route" "dose_vbm" "dechal" "rechal" "lot_num" "exp_dt" "exp_dt_num" "nda_num"
filenames <- list.files(pattern="^indi.*.csv", full.names=TRUE)
cat('We have downloaded the following quarterly diagnoses/indications datasets:\n')
filenames
indi=lapply(filenames,fread)
cat('\n')
cat('Variable names:\n')
names(indi[[15]])
indi_all=do.call(rbind,lapply(1:length(indi), function(i) select(as.data.frame(indi[i]),primaryid,caseid, indi_drug_seq,indi_pt)))
"./indi2012q1.csv" "./indi2012q2.csv" "./indi2012q3.csv" "./indi2012q4.csv" "./indi2013q1.csv" "./indi2013q2.csv" "./indi2013q3.csv" "./indi2013q4.csv" "./indi2014q1.csv" "./indi2014q2.csv" "./indi2014q3.csv" "./indi2014q4.csv" "./indi2015q1.csv" "./indi2015q2.csv" "./indi2015q3.csv"
"primaryid" "caseid" "indi_drug_seq" "indi_pt"
filenames <- list.files(pattern="^outc.*.csv", full.names=TRUE)
cat('We have downloaded the following quarterly patient outcome datasets:\n')
filenames
outc_all=lapply(filenames,fread)
cat('\n')
cat('Variable names\n')
names(outc_all[[1]])
names(outc_all[[4]])
colnames(outc_all[[4]])=c("primaryid", "caseid", "outc_cod")
outc_all=do.call(rbind,lapply(1:length(outc_all), function(i) select(as.data.frame(outc_all[i]),primaryid,outc_cod)))
"./outc2012q1.csv" "./outc2012q2.csv" "./outc2012q3.csv" "./outc2012q4.csv" "./outc2013q1.csv" "./outc2013q2.csv" "./outc2013q3.csv" "./outc2013q4.csv" "./outc2014q1.csv" "./outc2014q2.csv" "./outc2014q3.csv" "./outc2014q4.csv" "./outc2015q1.csv" "./outc2015q2.csv" "./outc2015q3.csv"
"primaryid" "outc_cod"
"primaryid" "caseid" "outc_code"
filenames <- list.files(pattern="^reac.*.csv", full.names=TRUE)
cat('We have downloaded the following quarterly reaction (adverse event) datasets:\n')
filenames
reac=lapply(filenames,fread)
cat('\n')
cat('Variable names:\n')
names(reac[[3]])
reac_all=do.call(rbind,lapply(1:length(indi), function(i) select(as.data.frame(reac[i]),primaryid,pt)))
"./reac2012q1.csv" "./reac2012q2.csv" "./reac2012q3.csv" "./reac2012q4.csv" "./reac2013q1.csv" "./reac2013q2.csv" "./reac2013q3.csv" "./reac2013q4.csv" "./reac2014q1.csv" "./reac2014q2.csv" "./reac2014q3.csv" "./reac2014q4.csv" "./reac2015q1.csv" "./reac2015q2.csv" "./reac2015q3.csv"
"primaryid" "pt"
all=as.data.frame(list(Demography=nrow(demo_all),Drug=nrow(drug_all),
Indications=nrow(indi_all),Outcomes=nrow(outc_all),
Reactions=nrow(reac_all)))
row.names(all)='Number of rows'
all
# SQL版本號
sqldf("SELECT COUNT(primaryid)as 'Number of rows of Demography data' FROM demo_all;")
# R版本號
nrow(demo_all)
3554979
# SQL版本號
sqldf("SELECT *
FROM demo_all
LIMIT 6;")
# R版本號
head(demo_all,6)
R1=head(demo_all,6)
SQL1 =sqldf("SELECT * FROM demo_all LIMIT 6;")
all.equal(R1,SQL1)
TRUE
SQL2=sqldf("SELECT * FROM demo_all WHERE sex ='F';")
R2 = filter(demo_all, sex=="F")
identical(SQL2, R2)
TRUE
SQL3=sqldf("SELECT * FROM demo_all WHERE age BETWEEN 20 AND 25;")
R3 = filter(demo_all, age >= 20 & age <= 25)
identical(SQL3, R3)
TRUE
# SQL版本號
sqldf("SELECT sex, COUNT(primaryid) as Total FROM demo_all WHERE sex IN ('F','M','NS','UNK') GROUP BY sex ORDER BY Total DESC ;")
# R版本號
demo_all %>% filter(sex %in%c('F','M','NS','UNK')) %>% group_by(sex) %>%
summarise(Total = n()) %>% arrange(desc(Total))
SQL3 = sqldf("SELECT sex, COUNT(primaryid) as Total FROM demo_all GROUP BY sex ORDER BY Total DESC ;")
R3 = demo_all%>%group_by(sex) %>%
summarise(Total = n())%>%arrange(desc(Total))
compare(SQL3,R3, allowAll=TRUE)
TRUE
dropped attributes
SQL=sqldf("SELECT sex, COUNT(primaryid) as Total FROM demo_all WHERE sex IN ('F','M','NS','UNK') GROUP BY sex ORDER BY Total DESC ;")
SQL$Total=as.numeric(SQL$Total
pie3D(SQL$Total, labels = SQL$sex,explode=0.1,col=rainbow(4),
main="Pie Chart of adverse event reports by gender",cex.lab=0.5, cex.axis=0.5, cex.main=1,labelcex=1)
names(indi_all)
names(drug_all)
"primaryid" "indi_drug_seq" "indi_pt"
"primaryid" "drug_seq" "drugname" "route"
names(indi_all)=c("primaryid", "drug_seq", "indi_pt" ) # 使兩個數據集變量名一致
R4= merge(drug_all,indi_all, by = intersect(names(drug_all), names(indi_all))) # R版本號合併
R4=arrange(R3, primaryid,drug_seq,drugname,indi_pt) # R版本號排序
SQL4= sqldf("SELECT d.primaryid as primaryid, d.drug_seq as drug_seq, d.drugname as drugname, d.route as route,i.indi_pt as indi_pt FROM drug_all d INNER JOIN indi_all i ON d.primaryid= i.primaryid AND d.drug_seq=i.drug_seq ORDER BY primaryid,drug_seq,drugname, i.indi_pt") # SQL版本號
compare(R4,SQL4,allowAll=TRUE)
TRUE # 兩種方法等價
R5 = merge(reac_all,outc_all,by=intersect(names(reac_all), names(outc_all)))
SQL5 =reac_outc_new4=sqldf("SELECT r.*, o.outc_cod as outc_cod FROM reac_all r INNER JOIN outc_all o ON r.primaryid=o.primaryid ORDER BY r.primaryid,r.pt,o.outc_cod")
compare(R5,SQL5,allowAll = TRUE)
TRUE
# 繪製不一樣性別的年齡機率分佈密度圖
ggplot(sqldf('SELECT age, sex FROM demo_all WHERE age between 0 AND 100 AND sex IN ("F","M") LIMIT 10000;'), aes(x=age, fill = sex))+ geom_density(alpha = 0.6)
。。sql
)bash
ggplot(sqldf("SELECT d.age as age, o.outc_cod as outcome FROM demo_all d INNER JOIN outc_all o ON d.primaryid=o.primaryid WHERE d.age BETWEEN 20 AND 100 LIMIT 20000;"),aes(x=age, fill = outcome))+ geom_density(alpha = 0.6)
ggplot(sqldf("SELECT de.sex as sex, dr.route as route FROM demo_all de INNER JOIN drug_all dr ON de.primaryid=dr.primaryid WHERE de.sex IN ('M','F') AND dr.route IN ('ORAL','INTRAVENOUS','TOPICAL') LIMIT 200000;"),aes(x=route, fill = sex))+ geom_bar(alpha=0.6)
ggplot(sqldf("SELECT d.sex as sex, o.outc_cod as outcome FROM demo_all d INNER JOIN outc_all o ON d.primaryid=o.primaryid WHERE d.age BETWEEN 20 AND 100 AND sex IN ('F','M') LIMIT 20000;"),aes(x=outcome,fill=sex))+ geom_bar(alpha = 0.6)
demo1= demo_all[1:20000,]
demo2=demo_all[20001:40000,]
R6 <- rbind(demo1, demo2)
SQL6 <- sqldf("SELECT * FROM demo1 UNION ALL SELECT * FROM demo2;")
compare(R6,SQL6, allowAll = TRUE)
TRUE
R7 <- semi_join(demo1, demo2)
SQL7 <- sqldf("SELECT * FROM demo1 INTERSECT SELECT * FROM demo2;")
compare(R7,SQL7, allowAll = TRUE)
TRUE
R8 <- anti_join(demo1, demo2)
SQL8 <- sqldf("SELECT * FROM demo1 EXCEPT SELECT * FROM demo2;")
compare(R8,SQL8, allowAll = TRUE)
TRUE
假設你有不論什麼建議和意見,請在下方留言。markdown
我十分敬佩做者能走完這個及其枯燥的流程。數據結構
但我不想再翻譯第二篇這樣的風格的文章了。。app
。ide