我有一个大型数据表,我想将其转换为数组。数组应重新排序数据表,以便匹配列唯一值的所有观察值都作为附加列表/向量提供。
我将提供一个手动编写的.js示例,我的意思如下,如果不清楚,但这是我尝试用R使用较小的虚拟数据集:
library(dplyr)
#get random data
test <- read.table("http://www.ats.ucla.edu/stat/data/test.txt", header = TRUE)
print(test)
#Remove duplicate values of test & unwanted columns and save in new data.frame (test2)
test2 <- select(test[!duplicated(test[,5]),], 5:6)
print(test2)
我想要做的是创建一个数组,通过将test $ schtyp的值与test2 $ schtyp的值相关联,将测试的观察值添加为列表/向量。
有人可以帮帮我吗?这就是我努力创造的目标
var JSON = [ {
"latitude":40.621111,
"longitude":-80.435278,
"countryName":"United States",
"countryCode":"USA",
"reactors": [
{
"reactorUnit":"SHIPPINGPORT",
"latitude":40.621111,
"longitude":-80.435278,
"type":"PWR",
"unitPower":60,
"capacity":68,
"status":"Permanent Shutdown",
"operator":"DOE DUQU",
"reactorSupplier":"WH",
"license":null,
"construction":19725,
"criticality":20821,
"grid":21156,
"commercial":21331,
"shutdown":30225
},
{
"reactorUnit":"DOUNREAY DFR",
"latitude":58.57814,
"longitude":-3.75233,
"type":"FBR",
"unitPower":11,
"capacity":15,
"status":"Permanent Shutdown",
"operator":"UKAEA",
"reactorSupplier":"UKAEA",
"license":null,
"construction":20149,
"criticality":21868,
"grid":22920,
"commercial":22920,
"shutdown":28185
}
]},{
"latitude":44.143333,
"longitude":4.709444,
"countryName":"France",
"countryCode":"FRA",
"reactors": [
{
"reactorUnit":"G-2 (MARCOULE)",
"latitude":44.143333,
"longitude":4.709444,
"type":"GCR",
"unitPower":39,
"capacity":43,
"status":"Permanent Shutdown",
"operator":"COGEMA",
"reactorSupplier":"SACM",
"license":null,
"construction":20149,
"criticality":21387,
"grid":21662,
"commercial":21662,
"shutdown":29253
},
{
"reactorUnit":"CALDER HALL-3",
"latitude":54.4205,
"longitude":-3.4975,
"type":"GCR",
"unitPower":49,
"capacity":60,
"status":"Permanent Shutdown",
"operator":"SL",
"reactorSupplier":"UKAEA",
"license":null,
"construction":20302,
"criticality":21186,
"grid":21245,
"commercial":21306,
"shutdown":37711
},
{
"reactorUnit":"CALDER HALL-4",
"latitude":54.4205,
"longitude":-3.4975,
"type":"GCR",
"unitPower":49,
"capacity":60,
"status":"Permanent Shutdown",
"operator":"SL",
"reactorSupplier":"UKAEA",
"license":null,
"construction":20302,
"criticality":21520,
"grid":21641,
"commercial":21641,
"shutdown":37711
}
]
如果你不明白我想说的话,请告诉我。我很感激你的帮助!
最好,naibaf!
答案 0 :(得分:1)
如果在转换为JSON之前嵌套列,则可以执行所需操作:
library(tidyr)
library(jsonlite)
test %>% nest(prgtype:ses) %>% toJSON() %>% prettify()
返回
[
{
"schtyp": 1,
"level": 1,
"data": [
{
"prgtype": "general",
"gender": 0,
"id": 70,
"ses": 4
}
]
},
{
"schtyp": 2,
"level": 1,
"data": [
{
"prgtype": "vocati",
"gender": 1,
"id": 121,
"ses": 4
},
{
"prgtype": "academic",
"gender": 0,
"id": 172,
"ses": 4
},
{
"prgtype": "academic",
"gender": 0,
"id": 113,
"ses": 4
},
{
"prgtype": "general",
"gender": 0,
"id": 50,
"ses": 3
},
{
"prgtype": "academic",
"gender": 0,
"id": 11,
"ses": 1
}
]
},
{
"schtyp": 3,
"level": 1,
"data": [
{
"prgtype": "general",
"gender": 0,
"id": 86,
"ses": 4
},
{
"prgtype": "vocati",
"gender": 0,
"id": 141,
"ses": 4
}
]
}
]