【问题标题】:Convert in R output of package Elastic (nested list?) to data.frame or JSON将包 Elastic(嵌套列表?)的 R 输出转换为 data.frame 或 JSON
【发布时间】:2015-05-28 09:56:07
【问题描述】:

我正在使用 R 和“elastic”包来查询包含 JSON 格式的 twitter 数据的弹性搜索数据库。查询工作正常,我得到了我期望的输出内容(out)。

class(out) 
[1] "list"

out$hits$hits 返回

> out$hits$hits
[[1]]
[[1]]$`_index`
[1] "twitter_all_geo-2014-11-01"

[[1]]$`_type`
[1] "ctweet"

[[1]]$`_id`
[1] "ubicity-twitter-160f0964-6fc7-43ef-af2a-0e1b8c8184c7"

[[1]]$`_version`
[1] 1

[[1]]$`_score`
[1] 2.10757

[[1]]$`_source`
[[1]]$`_source`$id
[1] "528330489049120770"

[[1]]$`_source`$created_at
[1] "2014-10-31T23:39:39+0000"

[[1]]$`_source`$user
[[1]]$`_source`$user$name
[1] "afterlifetemis"


[[1]]$`_source`$place
[[1]]$`_source`$place$geo_point 
[[1]]$`_source`$place$geo_point[[1]]
[1] 30.4529

[[1]]$`_source`$place$geo_point[[2]]
[1] 50.61104


[[1]]$`_source`$place$city
[1] "Ukraine"

[[1]]$`_source`$place$country
[1] "Ukraine"

[[1]]$`_source`$place$country_code
[1] "UA"

[[1]]$`_source`$msg
[[1]]$`_source`$msg$text
[1] "u had one job artemis\none"

[[1]]$`_source`$msg$lang
[1] "EN"

[[1]]$`_source`$msg$hash_tags
list()

[[2]]
[[2]]$`_index`
[1] "twitter_all_geo-2014-11-01"

[[2]]$`_type`
[1] "ctweet"
...
...

基本上我想将数据保存为.csv文件,所以我输入了

> write.csv(out$hits$hits,'out.csv')
Error in data.frame(text = "u had one job artemis\none", lang = "EN",   : arguments imply differing number of rows: 1, 0

我认为有必要将其转换为data.frame,所以我尝试了:

> df <- ldply (out, data.frame)

data.frame 中的错误(text = "你有一份工作 artemis\none", lang = "EN", : 参数暗示不同的行数:1, 0

(我尝试了其他几个,乐观主义者,尝试也像这个:)

> t(sapply(out$hits$hits, '[', 1:max(sapply(out$hits$hits, length))))
  _index                       _type    _id                                                        _version _score  _source
[1,] "twitter_all_geo-2014-11-01" "ctweet" "ubicity-twitter-160f0964-6fc7-43ef-af2a-0e1b8c8184c7" 1        2.10757 List,5 
[2,] "twitter_all_geo-2014-11-01" "ctweet" "ubicity-twitter-ba071fff-cafb-4d3f-947d-13c934905c1b" 1        2.10757 List,5 
[3,] "twitter_all_geo-2014-11-01" "ctweet" "ubicity-twitter-dd64af32-4d59-4008-a3db-74471ad269d1" 1        2.10757 List,5 
[4,] "twitter_all_geo-2014-11-01" "ctweet" "ubicity-twitter-4ba0d3d0-642d-4f9f-aaf9-c55929c35dc4" 1        2.10757 List,5 
[5,] "twitter_all_geo-2014-11-01" "ctweet" "ubicity-twitter-d7b8cbbc-87b3-44b5-8c9c-91c7b62f1458" 1        2.10757 List,5 
[6,] "twitter_all_geo-2014-11-01" "ctweet" "ubicity-twitter-76353a7c-44c9-4863-a59d-adb16716ca18" 1        2.10757 List,5 
[7,] "twitter_all_geo-2014-11-01" "ctweet" "ubicity-twitter-2aec0798-9918-4b66-9b2a-ef5a4d1f3711" 1        2.10757 List,5 
[8,] "twitter_all_geo-2014-11-01" "ctweet" "ubicity-twitter-c9e7637d-358a-40ee-a06c-85af04c22191" 1        2.10757 List,5 
[9,] "twitter_all_geo-2014-11-01" "ctweet" "ubicity-twitter-8928c1ef-f46a-4682-99c4-4dbc55270b03" 1        2.10757 List,5 
[10,] "twitter_all_geo-2014-11-01" "ctweet" "ubicity-twitter-d6b19975-b310-46c4-af11-af56971b7c4b" 1        2.10757 List,5 

一开始它看起来不错,但实际的推文消息已经不在矩阵中了

我很乐观,并认为可能先将其(返回)转换为 JSON(使用 RJSON)

toJSON(输出) toJSON(out) 中的错误:无法转义字符串。字符串不是 utf8

最后我有一个列表,无法保存,无法转换为 JSON、data.frame 或 data.table(因为它不统一)。有谁可以给​​我一个提示:a)将其转换为 JSON 或如何将列表保存到 .csv 文件或将其放入 data.frame 中?

非常感谢,我想我不明白。

-托比亚斯

【问题讨论】:

    标签: json r twitter elasticsearch


    【解决方案1】:

    我认为unlist()matrix() 可以胜任。

    Search()-return out 转换为数据框的示例:

    # get the first 3 hits from elasticsearch store
    out <- Search(index="shakespeare", size=3)
    
    # (optional) verify that all hits expand to the same length
    # (should be true for data intended to be in a table format)
    stopifnot(
        sapply(
            out$hits$hits, 
            function(x) {!(length(unlist(x)) - length(unlist(out$hits$hits[[1]])))}
        )
    )
    
    # count number of columns, use unlist() to convert 
    # nested lists to a vector, use the first hit as proxy
    nColumns <- length(unlist(out$hits$hits[[1]]))
    
    # fetch column names ... as above
    nNames <- names(unlist(out$hits$hits[[1]]))
    
    # unlist all hits and convert to matrix with ncol Columns, don't forget byrow=TRUE!
    df <- data.frame(matrix(unlist(out$hits$hits), ncol=nColumns, byrow=TRUE))
    
    # setting the column names
    names(df) <- nNames
    
    # do whatever you want with df
    print(df)
    

    干杯!

    【讨论】:

    • 谢谢!我必须尝试这种方法。我在中间所做的就是过滤掉消息,把它们放在一个列表中,然后就很容易了。但是您的方式实际上保存了完整的结果,并且是我一开始想要的。
    【解决方案2】:

    您可以在 R 中使用“jqr”包。例如:-

    datacsv<-jq(out,".hits.hits[] | @csv")   
    

    它将您的数据保存为 csv 格式,并且在“jqr”的帮助下,您还可以 grep 所需的字段。

    【讨论】:

      猜你喜欢
      • 2016-06-17
      • 2015-02-03
      • 1970-01-01
      • 2021-11-25
      • 2016-03-22
      • 2020-07-17
      • 1970-01-01
      • 2017-12-22
      • 2019-07-24
      相关资源
      最近更新 更多