Advertisement
Guest User

Untitled

a guest
Nov 16th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. require(RPostgreSQL)
  2. drv = dbDriver("PostgreSQL")
  3. con = dbConnect(drv, host = ..., post =..., dbname =..., user=..., password=...)
  4. df = dbGetQuery(con, query_string)
  5. dbDisconnect(con)
  6.  
  7. df = data.frame(id = c(1:100), arrcol = c(rep(paste0("{{",paste0(1:99,collapse=","),"}}"),10)))
  8.  
  9. df$arrcol = gsub(fixed=T, "{", "", df$arrcol)
  10. df$arrcol = gsub(fixed=T, "}", "", df$arrcol)
  11.  
  12. # Attempt 1:
  13. df$arrcol = as.numeric(df$arrcol)
  14. # Error: (list) object cannot be coerced to type 'double'
  15.  
  16. # Attempt 2:
  17. df$arrcol = lapply(df$arrcol, function(x) strsplit(x, ",", fixed=T))
  18. # no error, but now the data appears to be stored as a list of character lists:
  19. # arrcol[1]: list(c("1", "2", "3", "4", "5",...
  20.  
  21. # Attempt 3:
  22. df$arrcol = lapply(df$arrcol, function(x) as.numeric(unlist(strsplit(x, ",", fixed=T))))
  23. # this one seems to work
  24.  
  25. df$arrcol = lapply(df$arrcol, function(x) as.numeric(unlist(strsplit(x, ",", fixed=T))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement