应用于 json 输入的熔化函数用因子替换了一些值
melt function applied to json input replaces some values with factors
我有以下 json 文件:
{
"observations": [
{
"type": "ob_type_1",
"data": {
"dynamic": {
"sensor": [
{
"timestamp": 552625694285098,
"a": 1.4921862,
"b": 8.613739
},
{
"timestamp": 552625699285098,
"a": 0.6907272,
"b": 7.6243353
}
]
},
"static": {
"class1": "abc",
"class2": "xyz"
}
}
}
]
}
我使用 rjson 导入它:
library(rjson)
raw_json <- fromJSON(file=json_file)
要复制请使用:
raw_json <- structure(list(observations = list(structure(list(type = "ob_type_1",
data = structure(list(dynamic = structure(list(sensor = list(
structure(list(timestamp = 552625694285098, a = 1.4921862,
b = 8.613739), .Names = c("timestamp", "a", "b")),
structure(list(timestamp = 552625699285098, a = 0.6907272,
b = 7.6243353), .Names = c("timestamp", "a", "b")))), .Names = "sensor"),
static = structure(list(class1 = "abc", class2 = "xyz"), .Names = c("class1",
"class2"))), .Names = c("dynamic", "static"))), .Names = c("type",
"data")))), .Names = "observations")
然后我使用 reshape2 将其熔化:
library(reshape2)
reshape_json <- melt(raw_json)
然而,对于 'class1' 和 'class2',值列显示“1”和“2”。我希望它们是 'abc' 和 'xyz'。我怎样才能做到这一点?我过去曾尝试过 jsonlite,但当我的 json 文件有嵌套数组时,我很难获得正确的输出。
value L7 L6 L5 L4 L3 L2 L1
1 ob_type_1 <NA> NA <NA> <NA> type 1 observations
2 552625694285098 timestamp 1 sensor dynamic data 1 observations
3 1.4921862 a 1 sensor dynamic data 1 observations
4 8.613739 b 1 sensor dynamic data 1 observations
5 552625699285098 timestamp 2 sensor dynamic data 1 observations
6 0.6907272 a 2 sensor dynamic data 1 observations
7 7.6243353 b 2 sensor dynamic data 1 observations
8 1 <NA> NA class1 static data 1 observations
9 2 <NA> NA class2 static data 1 observations
options(stringsAsFactors = FALSE)
我有以下 json 文件:
{
"observations": [
{
"type": "ob_type_1",
"data": {
"dynamic": {
"sensor": [
{
"timestamp": 552625694285098,
"a": 1.4921862,
"b": 8.613739
},
{
"timestamp": 552625699285098,
"a": 0.6907272,
"b": 7.6243353
}
]
},
"static": {
"class1": "abc",
"class2": "xyz"
}
}
}
]
}
我使用 rjson 导入它:
library(rjson)
raw_json <- fromJSON(file=json_file)
要复制请使用:
raw_json <- structure(list(observations = list(structure(list(type = "ob_type_1",
data = structure(list(dynamic = structure(list(sensor = list(
structure(list(timestamp = 552625694285098, a = 1.4921862,
b = 8.613739), .Names = c("timestamp", "a", "b")),
structure(list(timestamp = 552625699285098, a = 0.6907272,
b = 7.6243353), .Names = c("timestamp", "a", "b")))), .Names = "sensor"),
static = structure(list(class1 = "abc", class2 = "xyz"), .Names = c("class1",
"class2"))), .Names = c("dynamic", "static"))), .Names = c("type",
"data")))), .Names = "observations")
然后我使用 reshape2 将其熔化:
library(reshape2)
reshape_json <- melt(raw_json)
然而,对于 'class1' 和 'class2',值列显示“1”和“2”。我希望它们是 'abc' 和 'xyz'。我怎样才能做到这一点?我过去曾尝试过 jsonlite,但当我的 json 文件有嵌套数组时,我很难获得正确的输出。
value L7 L6 L5 L4 L3 L2 L1
1 ob_type_1 <NA> NA <NA> <NA> type 1 observations
2 552625694285098 timestamp 1 sensor dynamic data 1 observations
3 1.4921862 a 1 sensor dynamic data 1 observations
4 8.613739 b 1 sensor dynamic data 1 observations
5 552625699285098 timestamp 2 sensor dynamic data 1 observations
6 0.6907272 a 2 sensor dynamic data 1 observations
7 7.6243353 b 2 sensor dynamic data 1 observations
8 1 <NA> NA class1 static data 1 observations
9 2 <NA> NA class2 static data 1 observations
options(stringsAsFactors = FALSE)