2016-05-17 11 views
0

私は配列に変換したい大きなデータテーブルを持っています。配列は、列の一意の値に一致するすべての観測値が追加リスト/ベクトルとして提供されるように、データテーブルを並べ替える必要があります。JSONのための多次元連想配列を構築する

これは不明であるが、ここで私は小さいダミーのデータセットを使用してRと試みたものです場合、私は、私は以下の何を意味するかの手動の.js書かれた例を提供します:

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の値に関連付けることによって、testの観測値がリスト/ベクトルとして追加される配列を作成することです。

誰かが私を助けることができますか?あなたは私が言うことをしようとしているのか理解していない場合はここで私は

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 
    } 
] 

を作成するために努力するものだ、私に知らせてください。私はこれについていくつかの助けに感謝したい!

ベスト、ナイバフ!

答えて

1

ネスト列をJSONに変換する前に、あなたがやりたいことができる場合:

[ 
    { 
     "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 
      } 
     ] 
    } 
] 
+1

感謝の房を

library(tidyr) library(jsonlite) test %>% nest(prgtype:ses) %>% toJSON() %>% prettify() 

戻ります!あなたは男です – Naibaf

関連する問題