将JSON列表转换为数据框

cqoc49vn  于 2023-02-26  发布在  其他
关注(0)|答案(2)|浏览(133)

我从下面的页面中提取了JSON:

library(jsonlite)
results <-  fromJSON("https://www.reddit.com/r/gardening/comments/1196opl/tree_surgeon_butchered_my_tree_will_it_be_ok/.json")
final = results$data

当我检查输出时,我可以看到,即使输出是"列表"格式,输出中似乎也有一个"表格数据框"结构:

t3, NA, gardening, , FALSE, NA, 0, FALSE, Tree surgeon butchered my tree - will it be ok?, r/gardening, FALSE, 6, NA, 0, 140, NA, all_ads, FALSE, t3_1196op
    • 我的问题:**基于以上所述,是否有可能以某种方式将此输出转换为 Dataframe ?

我尝试了以下代码:

dataframe_list = as.data.frame(final)

代码运行-但输出仍然不是表格/ Dataframe 输出。
最后,我希望得到以下格式的结果:

comment_id                      comment_text
1          1                 I like gardening!
2          2            I dont like to garden!
3          3             its too cold outside?
4          4 try planting something different?
5          5                    garden is fun!

有人能教我怎么做吗?
谢谢!

    • 也许我处理这个问题的方法不对,但有更好的方法?**
4ioopgfo

4ioopgfo1#

下面是使用pluck()bind_rows()unnest()的一种方法:

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

URL <- "https://www.reddit.com/r/gardening/comments/1196opl/tree_surgeon_butchered_my_tree_will_it_be_ok/.json"

fromJSON(URL) |>
  pluck("data", "children") |> # .$data$children
  bind_rows() |>
  filter(row_number() > 1) |>
  unnest(data) |>
  select(id, author, body) |>
  mutate(comment_id = row_number(), .before = "id")

输出:

# A tibble: 75 × 4
   comment_id id      author         body                                                                                             
        <int> <chr>   <chr>          <chr>                                                                                            
 1          1 j9ktvi3 mikpgod        "It'll grow back, probably won't be able to tell by summer. Except it'll be smaller"             
 2          2 j9l0egd hrudnick       "Saw a tree surgeons advert today. Said, \"Don't worry, I hug them first.\""                     
 3          3 j9kyb1v anonnewengland "It will be covered in new growth in a few months."                                              
 4          4 j9kqqqk Beatnikdan     "He must've been a civil war surgeon. \n\nThey should survive but get a different tree guy to cl…
 5          5 j9n0kp8 Live-Steaky    "Very few people in there comment section actually know what’s up. It’s a fine pruning job, extr…
 6          6 j9l2gxf Luke_low       "Speaking of Tree Butchery, My parents have hired an \"amateur landscaper guy\" a bunch of times…
 7          7 j9npnl1 tomt6371       "In all honesty it looks good,and definitely could have been pollarded further, it's the right s…
 8          8 j9kpkux Amezrou        "Had a tree surgeon round today to take the height off my Hazel and Plum trees and he’s absolute…
 9          9 j9kxjyz testhec10ck    "Those cut angles all look good. This seems pretty standard for an early spring pruning"         
10         10 j9laq63 MarieTC        "Lots of new growth will come and the tree will be fuller"                                       
# … with 65 more rows
lx0bsm1f

lx0bsm1f2#

为了解析来自Reddit的JSON,您可能需要检查RedditExtractoR包,get_thread_content()返回2个 Dataframe 的列表,一个用于线程,另一个用于注解:

library(dplyr)
thread <- RedditExtractoR::get_thread_content("https://www.reddit.com/r/gardening/comments/1196opl/tree_surgeon_butchered_my_tree_will_it_be_ok/")

thread$threads %>% 
  select(author, title, text) %>% 
  as_tibble()
#> # A tibble: 1 × 3
#>   author  title                                           text 
#>   <chr>   <chr>                                           <chr>
#> 1 Amezrou Tree surgeon butchered my tree - will it be ok? ""

thread$comments %>% 
  select(comment_id, author, comment) %>% 
  as_tibble()
#> # A tibble: 176 × 3
#>    comment_id  author           comment                                         
#>    <chr>       <chr>            <chr>                                           
#>  1 1           mikpgod          "It'll grow back, probably won't be able to tel…
#>  2 1_1         Amezrou          "I really hope so&"                             
#>  3 1_1_1       mikpgod          "Hazel's difficult to kill."                    
#>  4 1_1_1_1     symetry_myass    "&gt; Hazel's difficult to kill.\n\nI see an Um…
#>  5 1_1_1_2     Amezrou          "Yeah but what\u0019s it going to look like whe…
#>  6 1_1_1_2_1   EpidonoTheFool   "Not very good in my opinion a lot of weak grow…
#>  7 1_1_1_2_2   Cold-Pack-7653   "It will eventually look normal but its going t…
#>  8 1_1_1_2_3   lethal_moustache "Look for images of coppiced trees. Yours will …
#>  9 1_1_1_2_3_1 LeGrandePoobah   "This is an interesting article. I\u0019m not s…
#> 10 1_1_1_2_3_2 treecarefanatic  "this is pollarding not coppicing"              
#> # … with 166 more rows

创建于2023年2月23日,使用reprex v2.0.2

相关问题