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.
>Solution :
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)