【问题标题】:Turning data frame columns from list to character, causing SQLite error将数据框列从列表转换为字符,导致 SQLite 错误
【发布时间】:2016-04-23 01:13:06
【问题描述】:

我正在尝试将数据框添加到我的数据库并不断收到错误,我一开始遇到此错误,并找到了使用 tibble 的建议。

> dbWriteTable(db, "Wines", Wines, row.names=FALSE, overwrite=TRUE)
Error in (function (classes, fdef, mtable)  : unable to find an inherited method for function ‘dbWriteTable’ for signature ‘"SQLiteConnection", "character", "tbl_df"’

安装 tibble 包后,我现在收到此错误:

> dbWriteTable(db, "Wines", Wines, row.names=FALSE, overwrite=TRUE)
Error in sqliteSendQuery(conn, statement, bind.data) : RAW() can only be applied to a 'raw', not a 'character'

我的数据集是否有问题导致此问题?我以前从未遇到过 dbWriteTable() 的任何问题。

我认为部分问题是数据框中的 2 列是列表,我不确定如何转换它们。我尝试了 unlist,但它从我的数据框中删除了我尝试过的列。 数据是从网络抓取中获得的,所以这是我正在使用的一些内容。我的数据框有超过 1000 行。

Wines <- read.table(header = TRUE, stringsAsFactors = FALSE, text = 
"Winery Name Year   Price   Rating  Excerpt 'Charles Smith' 'Royal City Syrah'  
'2012'  '140'   '96'    'Green Olive, green stem' 'K Vintners'  'Cattle King Syrah' 
'2012'  '70'    '95'    'cranberry, dried herb, pomegranate' 'K Vintners'   
'Klein Syrah'   '2012'  '70'    '94'    'dark fruit, stemmy herb and olive' 
'Two Vintners'  'Make Haste Cinsault'   '2013'  '20'    '93'    '100% cinsault' 
'K Vintners'    'The Hidden Syrah'  '2012'  '70'    '93'    'fresh and dried herbs' 
'Kerloo'    'Stone Tree Malbec' '2013'  '40'    '93'    'dazzles' 'Bets Family' 
'Le Parrain Cabernet Sauvignon' '2012'  '135'   '93'    'rare cabernet' 'Kerloo' 
'Stone Tree Vineyard Cabernet Sauvignon'    '2013'  '50'    '93'    'high-toned herbs' 
'Effete'    'Big Papa Cabernet Sauvignon'   '2012'  '60'    '93'    'klispun and bacchus'")

当我运行 head 时,我得到的是:

head(Wines)
Source: local data frame [6 x 6]

 Winery      Name  Year price rating                                                                              excerpt
 <list>    <list> <chr> <chr>  <chr>                                                                                <chr>
1 <chr [1]> <chr [1]>  2012   140     96                   Green olive, green stem and fresh herb aromas are at the fore, ...
2 <chr [1]> <chr [1]>  2012    70     95 The kirsch, cranberry, dried herb, pomegranate and barrel spice aromas are laser ...
3 <chr [1]> <chr [1]>  2012    70     94               Brooding dark fruit and stemmy herb and olive aromas lead to rich, ...
4 <chr [1]> <chr [1]>  2013    20     93              This is a rare, 100% varietal Cinsault from Olsen Vineyard that saw ...
5 <chr [1]> <chr [1]>  2012    70     93             Opening with aromas of fresh and dried herbs, this wine follows with ...
6 <chr [1]> <chr [1]>  2013    40     93          All varietal coming from two blocks of this vineyard, this wine dazzles ...

我真的觉得我需要做的就是将这些列从列表转换为字符,但不知道如何在将它们保留在数据框中的同时做到这一点

【问题讨论】:

  • Wines &lt;- read.table(... 给了我错误。你能发布前 6 行的 dput 吗?也许Wines[1:2] &lt;- lapply(Wines[1:2], unlist)

标签: r list dataframe


【解决方案1】:

这是一个非常简单的修复。

Wines$Winery <- as.character(Wines$Winery)
Wines$Name <- as.character(Wines$Name)

这将指定列中的所有内容更改为字符而不是列表。

> head(Wines)
Source: local data frame [6 x 6]

      Winery                  Name  Year price rating
       <chr>                 <chr> <chr> <chr>  <chr>
1 Charles Smith      Royal City Syrah   2012   140     96
2    K Vintners     Cattle King Syrah   2012    70     95
3    K Vintners           Klein Syrah   2012    70     94
4  Two Vintners   Make Haste Cinsault   2013    20     93
5    K Vintners      The Hidden Syrah   2012    70     93
6        Kerloo     Stone Tree Malbec   2013    40     93
Variables not shown: excerpt <chr>.

一旦我这样做了,我就能够成功地运行 dbWriteTable 而不会出现任何错误。

> dbWriteTable(db, "Wines", Wines, row.names=FALSE, overwrite=TRUE)
[1] TRUE

【讨论】:

    猜你喜欢
    • 2017-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-14
    • 1970-01-01
    • 1970-01-01
    • 2019-04-28
    相关资源
    最近更新 更多