0

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'))
  • 1
    `dcast(df, domain~feature, fun=length)` ? With base R: `aggregate(feature~domain,df,FUN=table)` – Frank Nov 09 '15 at 03:28
  • 1
    Relevant: http://stackoverflow.com/questions/11659128/how-to-use-cast-or-another-function-to-create-a-binary-table-in-r/11659636 - `aggregate(model.matrix(~ feature - 1, data=df), df["domain"], max)` – thelatemail Nov 09 '15 at 03:40
  • Thank you both, @Frank and @thelatemail! I figured I was missing something easy (fun=length) and your help totally nailed it! – Isaac Wyatt Nov 09 '15 at 19:37
  • ^@Frank @thelatemail – Isaac Wyatt Nov 09 '15 at 19:37

0 Answers0