I have the following dataframe:
df1 = data.frame(id = 1:4, desc=c("httpmethod=put&hobbies=22.33&utiliites=50.00&home=950.00&entertainment=40.00&redirecturl=&stamp=5%0D%0A++++", "httpmethod=put&hobbies=&utiliites=&home=600.00&entertainment=25.57&redirecturl=&stamp=5%0D%0A++++", "httpmethod=put&hobbies=0.00&utiliites=&home=1127.53&entertainment=50.00&redirecturl=&stamp=5%0D%0A++++", "httpmethod=put&hobbies=&utiliites=&home=&entertainment=&redirecturl=&stamp=5%0D%0A++++"), stringsAsFactors=FALSE)
Which gives:
| id | desc |
|---|---|
| 1 | httpmethod=put&hobbies=22.33&utiliites=50.00&home=950.00&entertainment=40.00&redirecturl=&stamp=5%0D%0A++++ |
| 2 | httpmethod=put&hobbies=&utiliites=&home=600.00&entertainment=25.57&redirecturl=&stamp=5%0D%0A++++ |
| 3 | httpmethod=put&hobbies=0.00&utiliites=&home=1127.53&entertainment=50.00&redirecturl=&stamp=5%0D%0A++++ |
| 4 | httpmethod=put&hobbies=&utiliites=&home=&entertainment=&redirecturl=&stamp=5%0D%0A++++ |
I'd like:
| id | hobbies | utilities | home | entertainment |
|---|---|---|---|---|
| 1 | 22.33 | 50.00 | 950.00 | 40.00 |
| 2 | NA | NA | 600.00 | 25.57 |
| 3 | 0.00 | NA | 1127.53 | 50.00 |
| 4 | NA | NA | NA | NA |
I have looked at lots of different things but can't seem to bring it all together. The code I have at the moment is as below, but I'm thinking there must be a more simple/eloquent way (e.g. get the column names from the string).
library(dplyr)
library(tidyr)
library(stringr)
df2 <- df1 %>%
separate(desc, c("http","hob", "utl", "hom", "ent", "redirect", "stamp"), sep = "&") %>%
mutate(hobbies = str_extract(hob, "\\d+\\.*\\d*")) %>%
mutate(utilities = str_extract(utl, "\\d+\\.*\\d*")) %>%
mutate(home = str_extract(hom, "\\d+\\.*\\d*")) %>%
mutate(entertainment = str_extract(ent, "\\d+\\.*\\d*")) %>%
select(-c("http","redirect", "stamp"))
I am quite new to R so some explanation of the steps would be good. I did get to the point where I split them but ended up with a list and didn't know what to do to get the values out of the list.
Thanks
hobbies=0.00instead ofhobbies0.00=?