0

Sorry if the title is confusing, but hopefully this example is clear.

I have a dataframe like this:

> dput(dt1)
structure(list(Variable = c("Apple", "Orange"), Value = c("12, 5", 
"15")), class = "data.frame", row.names = c(NA, -2L))

As you can see, the Value column contains a comma in the first row. I would like to separate this, so that the final dataframe looks like this:

> dput(dt2)
structure(list(Variable = c("Apple", "Apple", "Orange"), Value = c(12L, 
5L, 15L)), class = "data.frame", row.names = c(NA, -3L))

All other columns should duplicate any time we are separating the comma.

Jamie
  • 543
  • 7
  • Does this answer your question? [Split delimited strings in a column and insert as new rows](https://stackoverflow.com/questions/15347282/split-delimited-strings-in-a-column-and-insert-as-new-rows) – Jan Jul 18 '23 at 16:16
  • https://tidyr.tidyverse.org/reference/separate_rows.html – Jon Spring Jul 18 '23 at 16:26

2 Answers2

1

You can use tidyr::separate_rows()

dt1 <- structure(list(Variable = c("Apple", "Orange"), Value = c("12, 5", 
                                                          "15")), class = "data.frame", row.names = c(NA, -2L))


tidyr::separate_rows(dt1, Value)
#> # A tibble: 3 x 2
#>   Variable Value
#>   <chr>    <chr>
#> 1 Apple    12   
#> 2 Apple    5    
#> 3 Orange   15

Created on 2023-07-18 by the reprex package (v2.0.0)

nniloc
  • 4,128
  • 2
  • 11
  • 22
1

Please try alternatively separate_longer_delim which is now recommended to use by tidyr

library(tidyr)

df %>% separate_longer_delim(Value, delim = ',')

  Variable Value
1    Apple    12
2    Apple     5
3   Orange    15
jkatam
  • 2,691
  • 1
  • 4
  • 12