I have the following sample data:
df
val_str
fruit=apple,machine=crane
machine=crane
machine=roboter
fruit=apple
machine=roboter,food=samosa
df2
fruit machine food
apple crane NA
NA crane NA
NA roboter NA
apple NA NA
NA roboter samosa
How do I get from df to df2? Each unique value before the "=" should create a column and then the respective values belonging to this should be spread across the rows.
Code:
df <- data.frame(val_str = c("fruit=apple,machine=crane","machine=crane","machine=roboter", "fruit=apple", "machine=roboter,food=samosa"))
df2 <- data.frame(fruit = c("apple",NA,NA,"apple","NA"),
machine = c("crane","crane","roboter",NA,"roboter"),
food = c(NA,NA,NA,NA,"samosa"))