0

The main dataframe has a column "passings". It is the only nested variable in the main dataframe. Inside it, there are dataframes (an example a nested cell). In the nested cells, the number of rows varies, yet the number of columns is the same. The columns names are "date" and "title". What I need is to grab a respective date and put it in the main dataframe as a new variable if title is "Закон прийнято" ("A passed law" - translation).

I'm a newbie in coding. I will appreciate your help!

dataframe an example of a dataframe within a nested cell

Yuliia Zhaha
  • 111
  • 6
  • 2
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. (Pictures of data are not reproducible) – MrFlick Nov 18 '19 at 21:52
  • 1
    Thank you for you comment. Next time, I will do this. This time, I did not really knew how to do that. The reproducible example is posted below by @akrun – Yuliia Zhaha Nov 18 '19 at 22:26

1 Answers1

1

Here is an option where we loop over the 'passings' list column with map (based on the image, it would be a list of 2 column data.frame), filter the rows where the 'title' is "Закон прийнято" (assuming only a single value per row) and pull the 'date' column to create a new column 'date' in the original dataset

library(dplyr)
library(purrr)
df1 %>%
   mutate(date = map_chr(passings, ~ .x %>%
                                       filter(title == "Закон прийнято") %>%
                                       pull(date)))

#   id passed                                     passings       date
#1 54949   TRUE 2015-06-10, 2015-06-08, abcb, Закон прийнято 2015-06-08
#2 55009   TRUE  2015-06-10, 2015-09-08, bcb, Закон прийнято 2015-09-08

NOTE: It works as expected.

data

df1 <- structure(list(id = c(54949, 55009), passed = c(TRUE, TRUE), 
    passings = list(structure(list(date = c("2015-06-10", "2015-06-08"
    ), title = c("abcb", "Закон прийнято")), class = "data.frame", row.names = c(NA, 
    -2L)), structure(list(date = c("2015-06-10", "2015-09-08"
    ), title = c("bcb", "Закон прийнято")), class = "data.frame", row.names = c(NA, 
    -2L)))), row.names = c(NA, -2L), class = "data.frame")
Community
  • 1
  • 1
akrun
  • 874,273
  • 37
  • 540
  • 662