我有以下json文件。
[
{
"y": 1.544937286376953,
"x": 0.0736468505859375,
"z": 10.19739440917969,
"timestamp": 1413232199331.14
},
{
"y": 2.492466888427734,
"x": 0.7253915405273438,
"z": 11.33457962036133,
"timestamp": 1413232199831.21
}
]
此列表中的两个对象都转换为高值。 类似于下面的天气示例,例如以下两个对象:
{
"y": 1.544937286376953,
"x": 0.0736468505859375,
"z": 10.19739440917969,
"timestamp": 1413232199331.14
}
和
{
"y": 2.492466888427734,
"x": 0.7253915405273438,
"z": 11.33457962036133,
"timestamp": 1413232199831.21
}
如果它们同时出现,则设置另一个属性,例如Velocity为High。
如何将其写入类似下面的天气示例:
@relation weather
@attribute outlook {sunny, overcast, rainy}
@attribute temperature numeric
@attribute humidity numeric
@attribute windy {TRUE, FALSE}
@attribute play {yes, no}
@data
sunny,85,85,FALSE,no
sunny,80,90,TRUE,no
我的属性在哪里是对象列表。
我的属性是这样的 @attribute accelerator [{numeric,numeric,numeric},{numeric,numeric,numeric}]
有谁知道我该怎么办?我的问题实际上是否有意义?
答案 0 :(得分:2)
在我看来,你想做两件事:
我不知道arff文件是否支持#2。
以下是将JSON转换为arff的一些代码(#1) 在R:
library(RWeka)
library(rjson)
json = rjson::fromJSON('[{
"y": 1.544937286376953,
"x": 0.0736468505859375,
"z": 10.19739440917969,
"timestamp": 1413232199331.14
},
{
"y": 2.492466888427734,
"x": 0.7253915405273438,
"z": 11.33457962036133,
"timestamp": 1413232199831.21
}]')
str(json) # show internal representation
# replace nulls, optional
json <- lapply(json, function(x) {
x[sapply(x, is.null)] <- NA
unlist(x)
})
# convert to data frame
mydf <- data.frame(do.call("rbind", json))
# add some more attributes. I've just made up this business logic
mydf["accelerator"] = sqrt(mydf$x^2 + mydf$y^2 + mydf$z^2)
# here the new "accelerator" attribute is high if it is higher than 11
mydf["accelerator_high"] = ifelse(mydf["accelerator"]<=11,"No","Yes")
RWeka::write.arff(mydf, "myfile.arff")
生成的arff文件:
@relation R_data_frame
@attribute y numeric
@attribute x numeric
@attribute z numeric
@attribute timestamp numeric
@attribute accelerator numeric
@attribute accelerator_high string
@data
1.544937,0.073647,10.197394,1413232199331.13984,10.314025,No
2.492467,0.725392,11.33458,1413232199831.209984,11.628038,Yes