我正在使用R和包'elastic'來查詢包含JSON格式的twitter數據的彈性搜索數據庫。查詢工作正常,我得到的輸出內容(出),如我所料。將Elastic(嵌套列表?)包的R輸出轉換爲data.frame或JSON
class(out)
[1] "list"
進出$ $命中命中回報
> 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 =「你有一個job artemis \ none」,lang =「EN」,: 參數意味着不同的行數:1,
(我嘗試了幾個其他的optimistc嘗試太喜歡這一個:)
> 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
而且在開始的時候看起來很不錯,但實際的tweet消息不再在矩陣
我很樂觀,想,也許第一(後)將其轉換JSON(使用RJSON)
toJSON(out) toJSON中的錯誤(out):無法轉義字符串。字符串不是utf8
最後,我有一個列表,無法保存,無法轉換爲JSON,data.frame或data.table(因爲它不是統一的)。有沒有人可以給我提示a)將其轉換爲JSON或如何將列表保存爲.csv文件或將其放入data.frame中?
非常感謝,我想我不理解它。
-Tobias
謝謝!我必須嘗試這種方法。我之間做的是過濾出這些信息,把它們放在一個列表中,然後它很容易。但是你的方式實際上保存了完整的結果,並且是我一開始就想要的。 – tobiokanobi 2015-04-29 18:16:12