[datatable-help] NAs introduced by coercion in rbindlist()

patricknic patricknic at gmail.com
Thu Jan 3 21:30:35 CET 2013


Apologies, I forgot to switch the directories in the code. Corrected on
nabble and below.




# Directories 
tempwd <- tempdir() 
setwd(tempwd) 

# Packages 
library(dataframe) 
library(data.table) 
library(foreign) 

# Get blocks and coordinates 
state.fips <- as.character(c(paste0(0, c(1:2, 4:6, 8:9)), 10:13, 15:42,
44:51, 53:56)) 
tmpf <- tempfile(fileext=".zip") 
dtlist <- lapply(state.fips, function(fips) { 
  cat("State", fips, ":\t") 
  nm <- paste0("tl_2011_", fips, "_tabblock") 
  dbfname <- paste0(nm, ".dbf") 
  if (!file.exists(file.path(tempwd, dbfname))) { 
    cat("Downloading...\t") 
    url <- paste0("http://www2.census.gov/geo/tiger/TIGER2011/TABBLOCK/",
nm, ".zip") 
    download.file(url, destfile=tmp, quiet=FALSE) 
    unzip(tmp, exdir=tempwd) 
  } 
  del <- dir(tempwd, pattern=nm) 
  invisible(lapply(del[grep("dbf", del, invert=TRUE)], file.remove)) 
  cat("Reading...\t") 
  df <- read.dbf(dbfname, as.is=TRUE) 
  dt <- as.data.table(df) 
  cat("Done\n") 
  dt[, list(blockfips = GEOID, land_area = ALAND, water_area = AWATER, long
= as.numeric(INTPTLON), 
            lat = as.numeric(INTPTLAT))] 
}) 
b <- rbindlist(dtlist) 

### No NA problem: 
dtlist2 <- lapply(dtlist, as.data.frame) 
b2 <- do.call("rbind", dtlist2) 



--
View this message in context: http://r.789695.n4.nabble.com/NAs-introduced-by-coercion-in-rbindlist-tp4654576p4654577.html
Sent from the datatable-help mailing list archive at Nabble.com.


More information about the datatable-help mailing list