I have a dataframe that looks as follows:
| Rest of data frame | columnToSeparate |
|---|---|
| blablarow1 | {"info1":"row1string1","info2":"row1string2"} |
| blablarow2 | {"info1":"row2string1","info2":"row2string2"} |
What is the best way to extract this information as separate columns in my dataframe to get something like:
| Rest of data frame | info1 | info2 |
|---|---|---|
| blablarow1 | row1string1 | row1string2 |
| blablarow2 | row2string1 | row2string2 |
Here is code to create the dataframe
df <- data.frame(RestOfDF = c("blablarow1", "blablarow2"),
columnToSeparate = c('{"info1":"row1string1","info2":"row1string2"}',
'{"info1":"row2string1","info2":"row2string2"}'))
Update: My real column has lots of infoN fiels, so I am looking to do something as automatic as possible as the names are actually different. Something like {"mydogsname":"pinky","mycatsfood":"icecream"...}
I think this can also help:
library(dplyr)
library(stringr)
df %>%
magrittr::extract(1) %>%
bind_cols(str_extract_all(df$columnToSeparate, "(\\w+\\d+)(\\w+\\d+)", simplify = TRUE) %>%
as_tibble() %>%
setNames(c("info1", "info2")))
RestOfDF info1 info2
1 blablarow1 row1string1 row1string2
2 blablarow2 row2string1 row2string2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With