Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将嵌套列表转换为dataframe/datatable_R_Json_Dataframe_Datatable_Dplyr - Fatal编程技术网

将嵌套列表转换为dataframe/datatable

将嵌套列表转换为dataframe/datatable,r,json,dataframe,datatable,dplyr,R,Json,Dataframe,Datatable,Dplyr,我遇到了一个非常特殊的问题。我有一个带有JSON字段的数据库: # A tibble: 1 x 3 field1 field2 JSONfield <int> <chr> <chr> 1 43 stringgg "{\"typ\": \"Liste mit Punkten\", \"min~

我遇到了一个非常特殊的问题。我有一个带有JSON字段的数据库:

# A tibble: 1 x 3
   field1 field2        JSONfield
    <int> <chr>         <chr>                                                      
1      43 stringgg      "{\"typ\": \"Liste mit Punkten\", \"min~
我将收到:

# A tibble: 2 x 10
   field1 field2     JSONfield                typ   min   max   items.1 items.2  items.3
    <int> <chr>       <chr>                   <chr> <int> <int> <fct>   <fct>    <fct>  
1     43  stringgg    "{\"typ\": \"Liste mit~ List~ 0     1      first  second   third   
2     43  stringgg    "{\"typ\": \"Liste mit~ List~ 0     1      3       0        7   
另外,我必须处理的JSON对象有多达7个名称/值对的子集,这些名称/值对可能出现在对象中,也可能不出现在对象中,因此我正在寻找一个不太具体的解决方案

在这个问题上我非常感谢你的帮助

试试看

library(dplyr)
library(purrr)
library(tidyr)
library(jsonlite)

text <- '{"typ": "Liste mit Punkten",  
   "min": 0, 
   "max": 1, 
   "items": [["first", "second", "third"], 
             [3, 0, 7]]}'

dd <- data_frame(field1 = 43, field2 = "stringgg", JSONfield = text)

dd %>% 
  mutate(json = map(JSONfield, ~ fromJSON(.) %>%
      map_if(is.matrix, ~list(as.data.frame(.))) %>% as_tibble
  )) %>%
  unnest() %>%
  select(-JSONfield)
# # A tibble: 1 x 6
#   field1 field2   typ                 min   max items               
#    <dbl> <chr>    <chr>             <int> <int> <list>              
# 1     43 stringgg Liste mit Punkten     0     1 <data.frame [2 × 3]>

我们将
items
元素(矩阵)转换为数据帧,然后将其放入列表中。最后,我们将整个列表(包含
typ
min
max
)转换为一个TIBLE。

data.frame
data.table
在R中不能接受另一个
data.frame
data.table
作为列。然后将其另存为嵌套列表,或者将项目连接到一个字符串列中。
# A tibble: 2 x 10
   field1 field2     JSONfield                typ   min   max   items
    <int> <chr>       <chr>                   <chr> <int> <int> <list>  
1     43  stringgg    "{\"typ\": \"Liste mit~ List~ 0     1      <data.frame~ 
{"typ": "Liste mit Punkten",  
   "min": 0, 
   "max": 1, 
   "items": [["first", "second", "third"], 
             [3, 0, 7]]}
library(dplyr)
library(purrr)
library(tidyr)
library(jsonlite)

text <- '{"typ": "Liste mit Punkten",  
   "min": 0, 
   "max": 1, 
   "items": [["first", "second", "third"], 
             [3, 0, 7]]}'

dd <- data_frame(field1 = 43, field2 = "stringgg", JSONfield = text)

dd %>% 
  mutate(json = map(JSONfield, ~ fromJSON(.) %>%
      map_if(is.matrix, ~list(as.data.frame(.))) %>% as_tibble
  )) %>%
  unnest() %>%
  select(-JSONfield)
# # A tibble: 1 x 6
#   field1 field2   typ                 min   max items               
#    <dbl> <chr>    <chr>             <int> <int> <list>              
# 1     43 stringgg Liste mit Punkten     0     1 <data.frame [2 × 3]>
# $typ
# [1] "Liste mit Punkten"

# $min
# [1] 0

# $max
# [1] 1

# $items
#      [,1]    [,2]     [,3]   
# [1,] "first" "second" "third"
# [2,] "3"     "0"      "7"