作者peterwu76 (金冈)
看板R_Language
标题[问题]
时间Sun Jun 4 17:02:44 2017
[问题类型]:
程式谘询(我想用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