I've reached a block trying to get this to work despite trying variations on reshape, table, xtabs, dplyr, and other functions. Can someone please help me transform the below data frame into a dataframe with indicator variables?
The data frame I am working with (note google listed twice because it has two features):
df = data.frame(domain=c("google","google","yahoo","microsoft"),
feature=c("a","b","a","c"))
The result that I am looking for (note google listed once with both features):
dfResult = data.frame(domain=c("google","yahoo","microsoft"),
feature_a=c("1","1","0"),
feature_b=c("1","0","0"),
feature_c=c("0","0","1")
)
The closest I've been able to come is with dcast but doesn't recode the variables into binary indicators:
dcast(df, formula = domain ~ feature, value.var = 'feature'))