0

これは、リポジトリの github stats api データの構造です。dplyr および tidy_json ライブラリを使用して、リポジトリ内のすべてのユーザーのコミット数 (「c」)、削除 (「d」)、追加されたコード行 (「a」)、および対応する週 (「w」) をリストしています。 .

      {
        "total": 5,
        "weeks": [
          {
            "w": 1428192000,
            "a": 0,
            "d": 0,
            "c": 0
          },
          {
            "w": 1428796800,
            "a": 0,
            "d": 0,
            "c": 0
          }
        ],
        "author": {
          "login": "ttuser1234",
          "id": 111111111
        }
      },
      {
        "total": 18,
        "weeks": [    
          {
            "w": 1428192000,
            "a": 212,
            "d": 79,
            "c": 5
          },
          {
            "w": 1428796800,
            "a": 146,
            "d": 67,
            "c": 1
          }
        ],
        "author": {
          "login": "coder1234",
          "id": 22222222
        }
      }
}

週と著者のデータを別々に抽出することはできますが、それらを結合することはできません。

inp_file=read_json("The JSON file")
dat=as.tbl_json(inp_file)
dat%>%
  enter_object("weeks") %>%
  gather_array %>%
  spread_values(week=jstring("w"),add=jstring("a"),del=jstring("d"),comm=jstring("c"))


enter_object("author") %>%
  spread_values(handle=jstring("login"))

著者オブジェクトから週オブジェクトにジャンプして、そのうちの2つをリンクすることはできません。これを行う方法はありますか?どんな助けにも感謝します。

4

2 に答える 2

0

tidyjsonいいですが、この場合に必要かどうかはわかりません。これが、望ましい結果であると私が考えるものを達成する1つの方法です。

library(jsonlite)
library(dplyr)

df1 <- fromJSON(
  '
[
{
"total": 5,
"weeks": [
{
  "w": 1428192000,
  "a": 0,
  "d": 0,
  "c": 0
},
  {
  "w": 1428796800,
  "a": 0,
  "d": 0,
  "c": 0
  }
],
  "author": {
  "login": "ttuser1234",
  "id": 111111111
  }
  },
  {
  "total": 18,
  "weeks": [    
  {
  "w": 1428192000,
  "a": 212,
  "d": 79,
  "c": 5
  },
  {
  "w": 1428796800,
  "a": 146,
  "d": 67,
  "c": 1
  }
  ],
  "author": {
  "login": "coder1234",
  "id": 22222222
  }
  }
]
'
)

# now the weeks column will actually be nested data.frames
#  we can sort of join the weeks with the author information
#  like this

df_joined <- df1 %>%
  do(
    data.frame(
      .[["author"]],
      bind_rows(.[["weeks"]])
    )
  )
于 2015-09-18T11:27:18.643 に答える