[英]Convert a PostgreSQL integer[] array to a numeric list in R
我將PostgreSQL查詢的結果存儲為R中的data.frame
。其中一個“列”是integer[]
數組類型。 在R中,它由RPostgreSQL
包作為字符串導入。
如何在我的data.frame
字符串類型轉換為數字列表類型列(或單獨的數字列)?
require(RPostgreSQL)
drv = dbDriver("PostgreSQL")
con = dbConnect(drv, host = ..., post =..., dbname =..., user=..., password=...)
df = dbGetQuery(con, query_string)
dbDisconnect(con)
df = data.frame(id = c(1:100), arrcol = c(rep(paste0("{{",paste0(1:99,collapse=","),"}}"),10)))
df$arrcol = gsub(fixed=T, "{", "", df$arrcol)
df$arrcol = gsub(fixed=T, "}", "", df$arrcol)
# Attempt 1:
df$arrcol = as.numeric(df$arrcol)
# Error: (list) object cannot be coerced to type 'double'
# Attempt 2:
df$arrcol = lapply(df$arrcol, function(x) strsplit(x, ",", fixed=T))
# no error, but now the data appears to be stored as a list of character lists:
# arrcol[1]: list(c("1", "2", "3", "4", "5",...
# Attempt 3:
df$arrcol = lapply(df$arrcol, function(x) as.numeric(unlist(strsplit(x, ",", fixed=T))))
# this one seems to work
我個人最好的答案:
df$arrcol = lapply(df$arrcol, function(x) as.numeric(unlist(strsplit(x, ",", fixed=T))))
或者,(只要每個數組具有相同的長度),您可以使用此技巧( 將數據幀字符串列拆分為多個不同的列 )將字符串解析為單獨的列。 請注意, read.table
足夠聰明,可以將每個新變量識別為整數。
newdf = read.table(text = df$arrcol, header = F, sep = ",")
此外,您可以輕松地將這些列作為自己的列附加到原始data.frame
:
df = cbind(df, newdf)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.