Skip to main content

Posts

Showing posts from October, 2015

R gsub on read.fwf and Oracle

Load few columns from file to Oracle Load few columns from file to Oracle Problem: Load a text file directly into oracle, using R Previous attempt: Tried to load the file into a data frame, but RStudio started to hang. library(ROracle) con <- dbConnect(drv, username ="<dbuser>", password = "<dbpassword>", dbname="(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=<host>)(Port=<port>))(CONNECT_DATA=(SERVICE_NAME=<db_service>)))") dbWriteTable(con,"<TABLENAME>",data.frame(gsub("\\s*,$", "",as.matrix( read.fwf( "C:\\Users\\<username>\\<filename>", widths = c(18,31,31,31,12,14,11,6,26,15,1), comment.char = "",stringsAsFactors = FALSE, skip = 1 )[,c(1,7)] ))),append = TRUE) Comments on the Solution: using ROracle read.fwf outputs a data frame as.matrix converts the dataframe into a matrix for gsub gsub outputs a m...