低成本空气质量传感器(AQE)将其数据发送到opensensors.io服务器。每隔x秒发送一串信息(时间戳,污染物浓度等)。可以将结构检索为json文件。 Opensensors术语使用devices
,topics
,organizations
和payloads
。我已经弄清楚如何设置curl句柄并使用curl包下载csv文件。这是代码
curl_download(url = myURL2, destfile = "curlDownloadTest.csv", mode = "w", handle = myCurlHandle)
下载数据的示例位于https://github.com/GeraldCNelson/AQEAnalysis/commit/c6ee29545d07835c5a920bf2b37625adb78462aa
我在jsonlite包中使用fromJSON
来转换此
temp <- fromJSON("curlDownloadTest.csv", simplifyDataFrame = FALSE)
输出(temp
)是一个包含2个元素的大型列表 - 消息和下一个。 messages
包含所有数据; next
是用于获取下一组数据的链接(不是一次全部下载)。
消息列表由多个列表组成(每组上传数据一个);每组有五个元素 - device
,owner
,topic
,date
和payload
。 Payload是3的列表 - encoding
(总是chr utf-8),content-type
(总是chr&#34; application / json&#34;)和text
。文本列表看起来像是json格式(这里是#string string - &#34; {\&#34; serial-number \&#34;:\&#34; egg00802aaa019b0111 \&#34;,\ &#34;转换值\&#34;:69.52,\&#34;转换单元\&#34;:\&#34; degF \&#34)
我想将此数据重组为具有的数据框(或数据表)
作为列的日期信息和来自有效负载的测试信息作为剩余列(serial-number
,converted value
等...
我无法弄清楚如何将有效负载列表中的文本列表从其当前(json?)结构转换为可以转换为数据帧的内容。
答案 0 :(得分:2)
谢天谢地,一切都非常统一:
library(jsonlite)
library(dplyr)
df <- fromJSON("curlDownloadTest.csv")
bind_cols(
select(df$messages, device, owner, topic, date),
stream_in(textConnection(df$messages$payload$text), flatten=TRUE)
) -> df
glimpse(df)
## Observations: 742
## Variables: 14
## $ device <chr> "egg00802aaa019b0111", "egg00802aaa019b0111", "egg00802aaa019b0111", "...
## $ owner <chr> "wickeddevice", "wickeddevice", "wickeddevice", "wickeddevice", "wicke...
## $ topic <chr> "/orgs/wd/aqe/temperature/egg00802aaa019b0111", "/orgs/wd/aqe/humidity...
## $ date <chr> "2016-10-10T17:02:09.507Z", "2016-10-10T17:02:09.811Z", "2016-10-10T17...
## $ serial-number <chr> "egg00802aaa019b0111", "egg00802aaa019b0111", "egg00802aaa019b0111", "...
## $ converted-value <dbl> 63.20, 43.31, 0.52, -25.20, 63.70, 42.85, 0.53, -13.32, 64.01, 42.58, ...
## $ converted-units <chr> "degF", "percent", "ppb", "ppb", "degF", "percent", "ppb", "ppb", "deg...
## $ raw-value <dbl> 63.200000, 43.310000, 0.221252, -0.827832, 63.700000, 42.850000, 0.221...
## $ raw-instant-value <dbl> 63.48000, 43.07000, 0.22149, -0.82785, 63.91000, 42.66000, 0.22073, -0...
## $ raw-units <chr> "degF", "percent", "volt", "volt", "degF", "percent", "volt", "volt", ...
## $ sensor-part-number <chr> "SHT25", "SHT25", "NO2-B4-ISB", "3SP-O3-20-PCB", "SHT25", "SHT25", "NO...
## $ raw-value2 <dbl> NA, NA, 0.222732, NA, NA, NA, 0.222797, NA, NA, NA, 0.222460, NA, NA, ...
## $ raw-instant-value2 <dbl> NA, NA, 0.22330, NA, NA, NA, 0.22273, NA, NA, NA, 0.22341, NA, NA, NA,...
## $ compensated-value <dbl> NA, NA, 0.62, -25.25, NA, NA, 0.63, -13.37, NA, NA, 0.02, -18.08, NA, ...