下に示すテキストデータの"relative_humidity"の数値を全て抜き出したいのですがデータの抽出方法がよく分かりません。
JSON
1コード 2[ 3 { 4 "_id": "59c49978c6197e0006ca9b40", 5 "nodeID": 15, 6 "relative_humidity": 93.6, 7 "time": "2017-09-22T14:02:41.000000+09:00" 8 }, 9 { 10 "_id": "59c49982c6197e0006ca9b47", 11 "nodeID": 15, 12 "soil_moisture_content": 23.73, 13 "soil_temperature": 22.7, 14 "time": "2017-09-22T14:02:52.000000+09:00" 15 }, 16 { 17 "_id": "59c49984c6197e0006ca9b49", 18 "air_temperature": 21.9, 19 "nodeID": 15, 20 "time": "2017-09-22T14:02:59.000000+09:00" 21 }, 22 { 23 "_id": "59c4998ec6197e0006ca9b52", 24 "nodeID": 15, 25 "soil_moisture_content": 23.73, 26 "soil_temperature": 22.7, 27 "time": "2017-09-22T14:03:05.000000+09:00" 28 }, 29 { 30 "_id": "59c49991c6197e0006ca9b54", 31 "air_temperature": 21.9, 32 "nodeID": 15, 33 "time": "2017-09-22T14:03:12.000000+09:00" 34 }, 35 { 36 "_id": "59c4999cc6197e0006ca9b5b", 37 "nodeID": 15, 38 "soil_moisture_content": 23.81, 39 "soil_temperature": 22.7, 40 "time": "2017-09-22T14:03:18.000000+09:00" 41 }, 42 { 43 "ATPR": 500, 44 "_id": "59c4999ec6197e0006ca9b5f", 45 "nodeID": 15, 46 "time": "2017-09-22T14:03:25.000000+09:00" 47 }, 48 { 49 "_id": "59c499a8c6197e0006ca9b6e", 50 "nodeID": 15, 51 "soil_moisture_content": 23.73, 52 "soil_temperature": 22.7, 53 "time": "2017-09-22T14:03:30.000000+09:00" 54 }, 55 { 56 "_id": "59c499aac6197e0006ca9b74", 57 "amount_of_solar_radiation": 49, 58 "nodeID": 15, 59 "time": "2017-09-22T14:03:37.000000+09:00" 60 }, 61 { 62 "_id": "59c499b6c6197e0006ca9b85", 63 "nodeID": 15, 64 "soil_moisture_content": 23.81, 65 "soil_temperature": 22.7, 66 "time": "2017-09-22T14:03:45.000000+09:00" 67 }, 68 { 69 "_id": "59c499b9c6197e0006ca9b8b", 70 "nodeID": 15, 71 "time": "2017-09-22T14:03:51.000000+09:00", 72 "wind_speed": 1 73 }, 74 { 75 "_id": "59c499c3c6197e0006ca9b9e", 76 "nodeID": 15, 77 "soil_moisture_content": 23.73, 78 "soil_temperature": 22.7, 79 "time": "2017-09-22T14:03:57.000000+09:00" 80 }, 81 { 82 "_id": "59c499c5c6197e0006ca9ba2", 83 "nodeID": 15, 84 "time": "2017-09-22T14:04:03.000000+09:00", 85 "wind_direction": "0.0" 86 }, 87 { 88 "_id": "59c499cfc6197e0006ca9bb3", 89 "nodeID": 15, 90 "soil_moisture_content": 23.73, 91 "soil_temperature": 22.7, 92 "time": "2017-09-22T14:04:09.000000+09:00" 93 }, 94 { 95 "_id": "59c49bb3c6197e0006ca9d90", 96 "nodeID": 15, 97 "relative_humidity": 94, 98 "time": "2017-09-22T14:12:17.000000+09:00" 99 } 100] //以下省略しています。 101
###試したこと
R
1library("rjson") 2result <- fromJSON(file="hamaguchi.txt") 3print(result) 4json_data_frame <- as.data.frame(result) //結果をデータフレームに格納 5print(json_data_frame) 6json_data_frame["relative_humidity"] 7output <- unlist(sapply(result, function(x){return(x$relative_humidity)})) //relative_humidityの全ての値を抽出 8print(output)
###結果
[1] 93.6 94.0 95.5 96.1 98.9 96.7 96.9 95.7 95.9 96.3
[11] 93.9 94.1 95.2 95.6 96.4 97.0 97.1 96.9 96.8 97.0
//以下省略しています。
とrelative_humidityの全ての値は出力できるのですが、timeも同時に出力したいと考えています。
###補足情報(言語/FW/ツール等のバージョンなど)
開発環境はR version 3.4.0
OSはmacOSです。
回答の方よろしくお願いします。
分かりにくい箇所がありましたら気軽に質問してください。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/03/25 12:51