我刚刚开始使用名为RMySQL的R软件包,以便在我的计算机上解决一些内存限制。我试图在R中使用一个包含100列的矩阵(称为data.df),然后在具有“100选择2”(= 4950)列的SQL数据库上创建一个新表,其中每列是两列的线性组合初始矩阵中的列。到目前为止,我有这样的事情:
countnumber <- 1
con <- dbConnect(MySQL(), user = "root", password = "password", dbname = "myDB")
temp <- as.data.frame(data.df[,1] - data.df[,2])
colnames(temp) <- paste(pairs[[countnumber]][1], pairs[[countnumber]][2], sep = "")
dbWriteTable(con, "spreadtable", temp, row.names=T, overwrite = T)
for(i in 1:(n-1)){
for(j in (i+1):n){
if(!((i==1)&&(j==2))){ #this part excludes the first iteration already taken care of
temp <- as.data.frame(data.df[,i] - data.df[,j])
colnames(temp) <- "hola"
dbWriteTable(con, "spreadtable", value = temp, append = TRUE, overwrite = FALSE, row.names = FALSE)
countnumber <- countnumber + 1
}
}
}
我也尝试过使用RMySQL dbWriteTable with field.types建议的RMySQL :: dbWriteTable()的“field.types”参数。可悲的是,它并没有帮助我太多。
问题:
感谢您的帮助。
[编辑]:错误输出的代码:
names <- as.data.frame(index)
names <- t(names)
#dim(names) is 1 409
con <- dbConnect(MySQL(), user = "root", password = "password", dbname = "taylordatabase")
dbGetQuery(con, dbBuildTableDefinition(MySQL(), name="spreadtable", obj=names, row.names = F))
#I would prefer these to be double types with 8 decimal spaces instead of text
#dim(temp) is 1 409
temp <- as.data.frame(data.df[,1] - (ratios[countnumber]*data.df[,2]))
temp <- t(temp)
temp <- as.data.frame(temp)
dbWriteTable(con, name = "spreadtable", temp, append = T)
该表在数据库中成功创建(稍后我将更改变量类型),但dbWriteTable()行会产生错误:
Error in mysqlExecStatement(conn, statement, ...) :
RS-DBI driver: (could not run statement: Unknown column 'row_names' in 'field list')
[1] FALSE
Warning message:
In mysqlWriteTable(conn, name, value, ...) : could not load data into table
如果我稍作修改,我会收到不同的错误消息:
dbWriteTable(con, name = "spreadtable", temp, append = T, row.names = F)
和
Error in mysqlExecStatement(conn, statement, ...) :
RS-DBI driver: (could not run statement: Unknown column 'X2011_01_03' in 'field list')
[1] FALSE
Warning message:
In mysqlWriteTable(conn, name, value, ...) : could not load data into table
我只想将“名称”用作一堆列标签。他们最初是约会。我想要的实际数据是“临时”。
答案 0 :(得分:0)
使用4950行进行查询是可以的,问题是您需要哪些列。
如果你总是“选择*”,你最终会耗尽所有系统内存(如果表有100列)
如果遇到任何问题,为什么不给我们一些错误信息?