Hello everyone
I have a very particular problem with my dataset. One variable is a long string that is separated by a comma and then further by a semicolon. The problem I have is to find a particular key and extract the respective value.
Minimum Example:
code <- c("a;10,b;20,c;30","b;20,c;30","c;30,b;40","a;40,b;50")
var_1 <- c(1,4,7,10)
var_2 <- c(2,5,8,11)
df <- data.frame(code,var_1,var_2)
The String can vary in size (i.e,. there could be one with a,b,c,d,e,f in there). Let's say the codes that I want to find is "a" and "b" and I want to extract the value after the semicolon as value into a new column. In the case there are multiple keys, I want to take the sum of them. I already have success finding if a key even exists in the string by using the grepl command.
The final result should be something like this:
solution <- c(30,20,40,90)
df <- data.frame(df, solution)
Thanks!