I have a MySQL database with many large tables in the following format:
mysql> select * from Table1 limit 2;
+-------+----------+-------------+
| chrom | site | methylation |
+-------+----------+-------------+
| 1 | 10003581 | 76 |
| 1 | 10003584 | 0 |
+-------+----------+-------------+
I would like to create one large merged table in R that will contain all the sites covered with the methylation values for each table. For instance, if I had 4 mysql tables the R data frame would contain the following columns:
chrom site table1 table2 table3 table4
So far I have:
library(RMySQL)
#Open database
mydb = dbConnect(MySQL(), user='root', password='', dbname='DataBase')
#Create function to get values
GetVal <- function(TableName, ColumnName){
rs = dbSendQuery(mydb, paste("SELECT chrom, site, methylation FROM ", TableName))
data = fetch(rs, n=-1)
res <- rename(data, c("chrom" = "Chr", "site" = "start", "methylation" = ColumnName))
return(res)
}
Table1 <- GetVal("Table1", "Table1")
Table2 <- GetVal("Table2", "Table2")
Table3 <- GetVal("Table3", "Table3")
Table4 <- GetVal("Table4", "Table4")
I'd then merge all the tables together. However I figure there should be a faster and more efficient way of doing this.