R and postgreSQL table operation -
i have been working on function tries obtain of households @ least 1 la. when use package data.table, possible upload , run function results, due memory problems i'm using postgresql , here becomes problem.
year sample serial pernum wtper relate birthyr bplctry 2005 8406 1244876000 3 75 4 na 24040 2005 8406 1244877000 1 62 1 na 22010 2005 8406 1244877000 2 67 2 na 24040 2005 8406 1244878000 1 137 1 na 24040 2005 8406 1244878000 2 130 2 na 24040 2005 8406 1244878000 3 149 3 na 24040 > paises [1] 21080 21100 21130 22020 22030 22040 22050 22060 22070 22080 23010 23020 23030 23040 23050 23060 23100 23110 23130 23140
then, reading (works)...
create postgresql instance , create 1 connection.
m <- dbdriver("postgresql") con <- dbconnect(m, user="postgres", password="xxxx", dbname="ipums", host='localhost', port=5432) mig_db <- src_postgres(dbname = 'ipums', user = 'postgres', password = 'xxxx')
then, here tried obtain households 1 la. (that part works if read usa fread function data.table pckg, here code sql statement)
usa <- tbl(mig_db, sql('select * namerica')) paises.n <- fread('paises.csv',header=t, sep=',', data.table=f) paises <- paises.n$code
here problem, function returns logical vector (logical(0)) usa$latino:
usa$latino <- ifelse(usa$bplctry %in% paises, 'la', 'otro') la <- usa[usa$latino == 'la', ] id <- unique(la$serial) usa.new <- usa[usa$serial %in% id,]
you should try using dbgetquery function in rpostgresql library
dbgetquery(con, "select * namerica")
Comments
Post a Comment