将数据框列从列表转换为字符,导致SQLite错误

时间:2016-04-23 01:13:07

标签: r list dataframe

我正在尝试将数据框添加到我的数据库并继续收到错误,我最初收到此错误,并发现了使用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列是列表,我不知道如何转换它们。我尝试了取消列表,但它从我的数据框中删除了我尝试过的列。 这些数据是从网页抓取中获得的,所以这里有一些我正在使用的内容。我的数据框有超过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(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 ...

我真的觉得我需要做的就是将这些列从列表转换为字符,但我不确定如何将它们保存在数据帧中

1 个答案:

答案 0 :(得分: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