An option would be separate
library(tidyverse)
voivodeshipdf %>%
separate(voivodeship, into = c('state', 'newcol'), sep=",", remove = FALSE) %>%
select(-newcol)
Or extract
voivodeshipdf %>%
extract(volvodeship, into = 'state', '^([^,]+),.*', remove = FALSE)
or with word
voivodeshipdf %>%
mutate(state = word(volvodeship, 1, sep=","))
The issue in the OP's code is that is subsetting the list
with [1]
, which would select the first list
element as a list
with one vector and it is getting assigned to the column due to recycling
Instead, what we need is to extract the first element from the list
output of str_split
with map
or lapply
(map
would be more appropriate in tidyverse
context)
voivodeshipdf %>%
mutate(state = map_chr(str_split(voivodeship, ','), first))