This is great, thanks! I have another unrelated question. I'll create a new email for that one.
ben On Mon, Nov 7, 2011 at 4:16 PM, Gabor Grothendieck <ggrothendi...@gmail.com>wrote: > On Mon, Nov 7, 2011 at 5:34 PM, Ben quant <ccqu...@gmail.com> wrote: > > Hello, > > > > Using the RpgSQL package, there must be a way to get the row names into > the > > table automatically. In the example below, I'm trying to get rid of the > > cbind line, yet have the row names of the data frame populate a column. > > > >> bentest = matrix(1:4,2,2) > >> dimnames(bentest) = list(c('ra','rb'),c('ca','cb')) > >> bentest > > ca cb > > ra 1 3 > > rb 2 4 > >> bentest = cbind(item_name=rownames(bentest),bentest) > >> dbWriteTable(con, "r.bentest", bentest) > > [1] TRUE > >> dbGetQuery(con, "SELECT * FROM r.bentest") > > item_name ca cb > > 1 ra 1 3 > > 2 rb 2 4 > > > > > > The RJDBC based drivers currently don't support that. You can create a > higher level function that does it. > > dbGetQuery2 <- function(...) { > out <- dbGetQuery(...) > i <- match("row_names", names(out), nomatch = 0) > if (i > 0) { > rownames(out) <- out[[i]] > out <- out[-1] > } > out > } > > rownames(BOD) <- letters[1:nrow(BOD)] > dbWriteTable(con, "BOD", cbind(row_names = rownames(BOD), BOD)) > dbGetQuery2(con, "select * from BOD") > > -- > Statistics & Software Consulting > GKX Group, GKX Associates Inc. > tel: 1-877-GKX-GROUP > email: ggrothendieck at gmail.com > [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.