簡體   English   中英

將PostgreSQL integer []數組轉換為R中的數字列表

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM