Opposite of toString()

library(dplyr)
df <- tibble(x = c("abc", "xyz", "pqr")) %>% 
  mutate(given = toString(x)) %>% 
  slice(1) %>% 
  select(given)
df  
#> # A tibble: 1 x 1
#>   given        
#>   <chr>        
#> 1 abc, xyz, pqr

How can I get each element, which is separated by a comma, as a row?
Here is my desired output:

# A tibble: 3 x 1
  x    
  <chr>
1 abc  
2 xyz  
3 pqr 

df <- tibble(x = c("abc", "xyz", "pqr"))
:slight_smile:

1 Like

@startz imagine reading an excel file with only one cell containing hundreds of values, each separated by a comma. Will your solution work? No!

True, but that's not what you asked.

If not @startz 's solution then checking tidyr cheatsheet will make it quite easy:

df |>
tidyr::separate_rows(given, sep = ", ")
#> # A tibble: 3 × 1
#>   given
#>   <chr>
#> 1 abc  
#> 2 xyz  
#> 3 pqr

Created on 2022-02-23 by the reprex package (v2.0.1)

2 Likes

@gsapijaszko Many thanks! This is what I was looking for :slight_smile:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.