I'm looking to answer a similar question as this in R.
I'm working with a dataset including a variable that concatenates 30 values of a string with numeric values in parentheses. The separate combinations of strings and parenthetical numbers are comma-separated.
IMPORTANT: Sometimes, string values may be repeating.
For example, in df the var might be:
id var
1 Videos (10.1), Music (9.5), Games (8.3), Videos (1)
2 Videos (11.1), Dogs (10.5), Cats (8.4), Dogs (1)
3 Cars (12.1), Music (9.5), Games (8.5), Games (2)
4 Cars (14.1), Music (9.5), Dogs (8.6)
5 Horses (10.1), Antelope (9.5), Music (8.7)
6 Music (10.1), Videos (9.0), Games (8.9)
What I would like to produce is additional columns where each unique string value in var has its own column, and the value for that column is the number in parentheses (when available). Something that is a bit tricky (to me) is that when a string value (e.g. Videos) is repeated, I would like to sum the numeric values.
So, in the dataset I've produced, the ideal output would be:
id Videos Music Games Dogs Cats Cars Horses Antelope
1 11.1 9.5 8.3 NA NA NA NA NA
2 11.1 NA NA 11.5 8.4 NA NA NA
3 NA 9.5 10.5 NA NA 12.1 NA NA
4 NA 9.5 NA 8.6 NA 14.1 NA NA
5 NA 8.7 NA 8.6 NA NA 10.1 9.5
5 9.0 10.1 8.7 NA NA NA NA NA
Any thoughts on how one would go about doing this in R?
EDIT: Real data included below:
my_df<-data.frame(id=1:20, var= c("PeopleBlogs(2.88)", "Music(3.90)", "Entertainment(3.05),Music(5.10),Music(2.28)",
"Sports(1.02)", "NonprofitsActivism(0.20),FilmAnimation(0.58)",
"Music(3.60),Music(1.42),Music(7.60)", "GadgetsGames(0.52)",
"Music(9.17),PeopleBlogs(0.33),PeopleBlogs(1.58),Music(8.82),Entertainment(1.38),PeopleBlogs(0.45),PeopleBlogs(0.58),Entertainment(0.92),FilmAnimation(1.60),FilmAnimation(7.57),Music(2.28),Entertainment(3.18),Entertainment(4.98),Music(0.48),FilmAnimation(0.28),FilmAnimation(0.18),Entertainment(5.97),Entertainment(1.35)",
"FilmAnimation(2.42),GadgetsGames(3.92)", "PeopleBlogs(4.38),GadgetsGames(15.47)",
"Entertainment(3.52)", "PeopleBlogs(0.22),Music(1.15),PetsAnimals(3.50),PeopleBlogs(2.78),PeopleBlogs(3.27)",
"Music(2.05),PeopleBlogs(0.20)", "Music(3.48),Music(4.65),Music(0.55)",
"Entertainment(0.78)", "Entertainment(4.35),PeopleBlogs(2.33),Comedy(7.05),PeopleBlogs(7.27)",
"Entertainment(0.50)", "Education(1.73)", "Education(0.67)",
"GadgetsGames(17.35),Education(7.40),NewsPolitics(0.35)"))