2016-04-23 33 views
1

我想向我的数据库添加一个数据框并不断收到错误,我一开始就遇到了这个错误,并且发现了一个使用tibble的建议。将数据框列从列表转换为字符,导致SQLite错误

> 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(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 ... 

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

+0

的'葡萄酒< - 函数read.table(...'给错误我,你可以发布第一6行的dput也许'葡萄酒[1:2]。< - lapply(葡萄酒[1 :2],unlist)' – akrun

回答

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 
相关问题