[問題] Json to R and Data talbe/matrix arrange

看板R_Language作者 (金岡)時間8年前 (2017/06/04 17:02), 8年前編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/3 (看更多)
[問題類型]: 程式諮詢(我想用R 做某件事情,但是我不知道要怎麼用R 寫出來) [軟體熟悉度]: 初學者 有寫過其他程式的初淺經驗 [問題敘述]: 1. 想從3個json來源的其中一個source抓資料下來, 最後我只要其中一個device (ID: 74DA38C7D1D2 )的資訊 (1) 有全部的device資訊,real time https://data.lass-net.org/data/last-all-airbox.json (2) 一個是by specific ID,real time https://data.lass-net.org/data/last.php?device_id=74DA38C7D1D2 (3) 一個是historical by ID https://data.lass-net.org/data/history.php?device_id=74DA38C7D1D2 2. Data處理: 抓下來的資料依照時間排序,如果是 (1)的作法: 打算寫個抓下來需要再依照ID去做擷取並output的script, 再用工作排程器做定時抓資料,但排程器還沒掌握到如何 可以每天每小時/5分鐘去啟動script; (2)的做法: 就是寫好再用工作排程器作定時抓資料。 (3)的作法: 抓下來後,想辦法排序。 3. Problems: (1)的做法與(2)的做法: 用自己try出來的寫法,但排程器還沒掌握到如何可以每天每小時或 5分鐘去啟動script;或是本身用R做到這件事。 (3)的做法: 不確定怎麼克服 1 row by 多重columns 至 只要些specific columns in rows by different times 4. 希望眾前輩可以提供些function/package 的 keyword 含 concept。 非常感謝大家! [程式範例]: 看起來好閱讀? https://hackmd.io/KwU2CMDMENQWmAZgEwGM4BZrmXcB2AThDmn2GQBNxKAGDVYYIA== # Scenario 1: for real time library(“rjson”) library(“RCurl”) library(“jsonlite”) #Set data source url <- “https://data.lass-net.org/data/last-all-airbox.json” #Get data from Json file x <- fromJSON(url) #Convert json file into tables y <- as.data.frame(x) #Capture Device ID= "74DA38C7D1D2" final_data_id <- subset(y, feeds.SiteName %in% c(“74DA38C7D1D2”)) #Write into csv file final_data_id$Content <- NULL date <- sprintf("AIRBOX_%s.csv", format(Sys.time(),"%Y%m%d%H")) outfile = paste("D:/", date, sep="") write.table(final_data_id, file = outfile, quote = F, sep="," , row.names = F) # Scenario 2: for real time library(“rjson”) library(“RCurl”) library(“jsonlite”) #Set data source url <- “https://data.lass-net.org/data/last.php?device_id=74DA38C7D1D2” #Get data from Json file x <- fromJSON(url) #Convert json file into tables y <- as.data.frame(x) #Write into csv file final_data_id$Content <- NULL date <- sprintf("AIRBOX_%s.csv", format(Sys.time(),"%Y%m%d%H")) outfile = paste("D:/", date, sep="") write.table(y, file = outfile, quote = F, sep="," , row.names = F) # Scenario 3: Posterior library(“rjson”) library(“RCurl”) library(“jsonlite”) #Set data source url <- “https://data.lass-net.org/data/history.php?device_id=74DA38C7D1D2” #Get data from Json file x <- fromJSON(url) #Convert json file into tables y <- as.data.frame(x) #It looks like it has thausands of columns by different times #I don’t know how to arrange the columns by specific columes in rows by different time #Write into csv file write.csv(y, “D:\final_data.csv”) [環境敘述]: R studio [關鍵字]: 選擇性,也許未來有用, Json to R, Url to R, 資料介接, Data frame, Arrange ※ 編輯: peterwu76 (123.193.92.13), 06/04/2017 17:15:22 ※ 編輯: peterwu76 (123.193.92.13), 06/04/2017 17:33:58
文章代碼(AID): #1PCyp0Zq (R_Language)
文章代碼(AID): #1PCyp0Zq (R_Language)