我已成功將所有MySQL表加載到儘可能多的數據框變量中,所有的表名都與表名(這是個人學習)使用以下代碼相同。我的問題是:有沒有更好的方法,因爲我感覺這很慢。將所有MYSQL表加載到具有相同名稱的數據框中
db<-dbConnect(MySQL(),user='****',password="****",dbname="****",host='XX.XXX.XXX.XX')
tables<-dbListTables(db)
#load all tables in variables with same name as the MySQL table name.
#warning : this loop will take more than 90 seconds to complete and will download all the MySQL tables.
for (i in 1:NROW(tables)){
assign(tables[i],dbReadTable(db,tables[i]))
}
#release the DB
dbDisconnect(db)
這是地球上最快的方法嗎?
速度方面的限制因素不是R代碼,而是連接到數據庫和表的大小。然而,更好的方法是將所有數據框放在一個命名列表中(在表名上使用'lapply'),如果你剛剛學習R,忘記了聽過「assign」;不要使用它。 – joran
'dfList < - lapply(tables,function(t)dbReadTable(db,t))' – Parfait
@Parfait:這會將表存儲在一個數組(列表)中。但我需要它們的變量名稱與DB中的表相同。 –