Большой список JSON для аккуратной таблицы - PullRequest
3 голосов
/ 03 октября 2019

У меня есть эти данные:

list(list(TrainId = "434", TrainNumber = "602", CarCount = 6L, 
DirectionNum = 1L, CircuitId = 1117L, DestinationStationCode = "G05", 
LineCode = "SV", SecondsAtLocation = 43L, ServiceType = "Normal"), 
list(TrainId = "417", TrainNumber = "609", CarCount = 8L, 
    DirectionNum = 2L, CircuitId = 3021L, DestinationStationCode = "N06", 
    LineCode = "SV", SecondsAtLocation = 1L, ServiceType = "Normal"), 
list(TrainId = "023", TrainNumber = "309", CarCount = 8L, 
    DirectionNum = 2L, CircuitId = 2364L, DestinationStationCode = "C15", 
    LineCode = "YL", SecondsAtLocation = 28L, ServiceType = "Normal"), 
list(TrainId = "450", TrainNumber = "411", CarCount = 8L, 
    DirectionNum = 2L, CircuitId = 1260L, DestinationStationCode = "J03", 
    LineCode = "BL", SecondsAtLocation = 2L, ServiceType = "Normal"), 
list(TrainId = "417", TrainNumber = "609", CarCount = 8L, 
    DirectionNum = 2L, CircuitId = 3021L, DestinationStationCode = "N06", 
    LineCode = "SV", SecondsAtLocation = 1L, ServiceType = "Normal"))

Данные пришли из JSON, и я получил их от простого list.files, а затем lst <- lapply(files, fromJSON)

Что лучше tidyverse способ получить его в кадре данных? Тот, который имеет TrainID, TrainNumber, LineCode и т. Д. В качестве заголовков столбцов ( не в качестве строк)? Мне не повезло с melt в сочетании с lapply. Также попробовал это:

table <- for(i in lst) {
     melt(i)
   }

Это возвращает один ряд того, что я хочу, но предпочел бы оставить его в семействе apply. Я НЕ ищу ниже, что получается из простого melt(lst[1]):

 structure(list(value = c("417", "609", "8", "2", "3021", "N06", 
 "SV", "1", "Normal"), L2 = c("TrainId", "TrainNumber", "CarCount", 
 "DirectionNum", "CircuitId", "DestinationStationCode", "LineCode", 
 "SecondsAtLocation", "ServiceType"), L1 = c(1L, 1L, 1L, 1L, 1L, 
  1L, 1L, 1L, 1L)), row.names = c(NA, -9L), class = "data.frame")

Ответы [ 2 ]

1 голос
/ 04 октября 2019

Вот еще одно решение с использованием purrr и base:

as.data.frame(map(split.default(unlist(a), names(unlist(a))), function(x) reduce(x,rbind)))

Обратите внимание на использование split.default и reduce. Первая функция разбивает незарегистрированный объект, в то время как reduce используется для привязки строки к элементам столбца, которые скоро станут.

1 голос
/ 03 октября 2019

Мы можем использовать unnest_wider

library(tidyr)
library(dplyr)
tibble(col1 = lst1) %>% 
          unnest_wider(col1)
# A tibble: 5 x 9
#  TrainId TrainNumber CarCount DirectionNum CircuitId DestinationStationCode LineCode SecondsAtLocation ServiceType
#  <chr>   <chr>          <int>        <int>     <int> <chr>                  <chr>                <int> <chr>      
#1 434     602                6            1      1117 G05                    SV                      43 Normal     
#2 417     609                8            2      3021 N06                    SV                       1 Normal     
#3 023     309                8            2      2364 C15                    YL                      28 Normal     
#4 450     411                8            2      1260 J03                    BL                       2 Normal     
#5 417     609                8            2      3021 N06                    SV                       1 Normal    

Или используя base R

do.call(rbind, lapply(lst1, as.data.frame))
#TrainId TrainNumber CarCount DirectionNum CircuitId DestinationStationCode LineCode SecondsAtLocation ServiceType
#1     434         602        6            1      1117                    G05       SV                43      Normal
#2     417         609        8            2      3021                    N06       SV                 1      Normal
#3     023         309        8            2      2364                    C15       YL                28      Normal
#4     450         411        8            2      1260                    J03       BL                 2      Normal
#5     417         609        8            2      3021                    N06       SV                 1      Normal
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...