Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 206473

How to pull out pairs of data from a list inside a dataframe (with tidyverse)

$
0
0

I have a json output (from topydo) which has (after some manipulation) a column called tags where the tags (if they are present) are pairs of data in a list. Usually they will be a due date "due", sometimes a repeat date "rec", and sometimes both. Reproducible example below which produces a dataframe with a 'tags' column containing lists the lists of due/rec pairs.

I want to pull these lists apart (akin to pivot_wider or spread) but for the elements in the list, so I want to turn this list item:

[[1]]
     [,1]  [,2]        
[1,] "rec""+6m"       
[2,] "due""2020-02-02"

into two columns - rec and due.

This is what the 'tags' column looks like:

> todo.df$tags
[[1]]
     [,1]  [,2]        
[1,] "rec""+6m"       
[2,] "due""2020-02-02"

[[2]]
list()

[[3]]
     [,1]  [,2]        
[1,] "due""2020-03-01"
[2,] "rec""+1y"       

[[4]]
     [,1]  [,2]        
[1,] "due""2020-05-01"

I have tried a variety of the unnest functions but can't work out how to pull the due dates and the rec date into their own columns.

Any help appreciated.


Reproducible example:

library(jsonlite)
library(dplyr)
todo.df <- "[{\"contexts\": [], \"priority\": null, \"projects\": [], \"source\": \"organise website review.  rec:+6m due:2020-02-02\", \"tags\": [[\"rec\", \"+6m\"], [\"due\", \"2020-02-02\"]], \"text\": \"organise website review.\"}, {\"contexts\": [\"smtl\", \"jim\"], \"priority\": \"B\", \"projects\": [], \"source\": \"(B) create wiki page on installing packages @jim @smtl\", \"tags\": [], \"text\": \"create wiki page on installing packages @jim @smtl\"}, {\"contexts\": [\"smtl\", \"jim\"], \"priority\": null, \"projects\": [], \"source\": \"Joomla mobile interface @smtl @jim due:2020-03-01 rec:+1y\", \"tags\": [[\"due\", \"2020-03-01\"], [\"rec\", \"+1y\"]], \"text\": \"Joomla mobile interface @smtl @jim\"}, {\"contexts\": [\"smtl\", \"jim\"], \"priority\": null, \"projects\": [], \"source\": \"review wiki documents @smtl @jim due:2020-05-01\", \"tags\": [[\"due\", \"2020-05-01\"]], \"text\": \"review wiki documents @smtl @jim\"}]" %>% 
  fromJSON(simplifyDataFrame = TRUE) %>%
  as_tibble() %>% 
  select (priority, contexts, projects, tags, text)

Viewing all articles
Browse latest Browse all 206473

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>