Skip to content

Instantly share code, notes, and snippets.

@pr130
Created September 1, 2020 15:05
Show Gist options
  • Save pr130/11e684797ce4e7e1bd5eb9a71f78a5d9 to your computer and use it in GitHub Desktop.
Save pr130/11e684797ce4e7e1bd5eb9a71f78a5d9 to your computer and use it in GitHub Desktop.
Python character list to rows
library(dplyr)
library(tibble)
library(stringr)
library(tidyr)
py_list_vec <- c('["foo", "bar", "baz"]', '["test1", "tst2"]', '["a", "b", "c", "d", "e"]')
df <- tibble::tibble(id = c(1, 2, 3), x = py_list_vec)
df %>%
dplyr::mutate(x = stringr::str_replace(x, '\\[', "")) %>%
dplyr::mutate(x = stringr::str_replace(x, '\\]', "")) %>%
tidyr::separate_rows(x, sep = ",")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment