I have this dataframe:
df <- data.frame(
option_label_1 = c("thickness", "strength", "color"),
option_value_1 = c("0.5 in", "2 lb" , "red"),
option_label_2 = c("size", "color", "thickness"),
option_value_2 = c("0.5 Inches x 7200 Feet", "blue" , "1 in"),
option_label_3 = c("stretch", NA, NA),
option_value_3 = c("wide", NA , NA)
)
option_label_1 option_value_1 option_label_2 option_value_2 option_label_3 option_value_3
1 thickness 0.5 in size 0.5 Inches x 7200 Feet stretch wide
2 strength 2 lb color blue <NA> <NA>
3 color red thickness 1 in <NA> <NA>
I want this data frame:
option_label_1 option_value_1 option_label_2 option_value_2 option_label_3 option_value_3
1 thickness 0.5 in size 0.5 Inches x 7200 Feet stretch wide
2 strength 2 lb color blue <NA> <NA>
3 color red thickness 1 in <NA> <NA>
json
1 {"thickness":"0.5 in","size":"0.5 Inches x 7200 Feet","stretch":"wide"}
2 {"strength":"2 lb","color":"blue"}
3 {"color":"red","thickness":"1 in"}
Essentially I want a JSON column added to the original df built off of the original columns using the option labels and option values. Please note I do not want a solution that converts the whole dataframe to JSON using toJSON. I have a much larger dataframe with other fields I do not want in JSON. I just want the option_labels and their respective option_values to be in JSON.
I have tried using list and paste functions nested in toJSON, but the "option_labels" are static and don't change accordingly in the resulting JSON column.
Thanks for your help!