data.frame - How to expand a data frame factor column into one column per level in R? -
the goal i'm trying accomplish take info frame column factor, create new column each level , populate column appropriate value level original info frame.
here sample. in case, want create new column each level of the.name factor column, so:
original dataframe:
symbol the.name cn sym1 abc 1 sym2 abc 2 sym1 def 3 sym2 def 4 resulting dataframe:
symbol abc def sym1 1 3 sym2 2 4 how can done?
edit: have tried accomplish using sapply loop split column , thenrbinding results. however, have not gotten work , chose not add together question generate noise - i'm pretty sure method not right , can considerably improved.
this reshaping task (from long wide data). bundle reshape2 has great utilities this.
txt="symbol the.name cn sym1 abc 1 sym2 abc 2 sym1 def 3 sym2 def 4" tmp <- read.table(text=txt, header=true) library(reshape2) dcast(tmp, symbol ~ the.name) ## easy r data.frame calculated-columns
No comments:
Post a Comment