【问题标题】:Transforming a list in a dataframe to a character将数据框中的列表转换为字符
【发布时间】:2015-12-10 21:25:38
【问题描述】:

我有一个从 JSCON 抓取的数据框。

store <- fromJSON(txt='https://openapi.etsy.com/v2/shops/epuu/listings/active?api_key=Redacted')
store_json <- as.data.frame(store$results)

但是,当我导出数据框store_json 时会产生错误

Error in .External2(C_writetable, x, file, nrow(x), p, rnames, sep, eol,  : 
  unimplemented type 'list' in 'EncodeElement'

现在我明白这是因为 data.frame 中有一个列表。当我检查向量时,我发现有列表,其中包含 c("Jewelry", "Earrings", "Dangle &amp; Drop Earrings") 等元素。当我尝试使用unlist(vector) 时,它不起作用。 gsub 会工作吗?也许粘贴功能然后删除c()?我已经尝试过tostring,虽然当我导出为 csv 时矢量确实变成了一个字符,但它不能正常工作。

【问题讨论】:

标签: r


【解决方案1】:

如果你使用的是 jsonlite 包...

http://finzi.psych.upenn.edu/library/jsonlite/html/unbox.html

你可以这样做..我相信有更好的方法..

store <- fromJSON(txt='https://openapi.etsy.com/v2/shops/epuu/listings/active?api_key=Redacted', flatten = TRUE)
z <- toJSON(store, auto_unbox = TRUE)
store_json <- as.data.frame(z$results)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-28
    • 1970-01-01
    • 2022-12-11
    • 2019-09-14
    相关资源
    最近更新 更多