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
startz
2
df <- tibble(x = c("abc", "xyz", "pqr"))
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!
startz
4
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
system
Closed
7
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.