r - How do I split a column of factors into multiple columns of multiple factors? -
say have data frame looks this:
factor value ====== ===== 1a.in 1.0 1a.out 2.6 1b.in 0.5 1b.out 3.4 2a.in 5.5
etc.
my goal add columns data frame extract information single factor column, such:
factor value fact1 fact2 fact3 ====== ===== ===== ===== ===== 1a.in 1.0 1 in 1a.out 2.6 1 out 1b.in 0.5 1 b in 1b.out 3.4 1 b out 2a.in 5.5 2 in
i cannot figure out how without using subset ad nauseum. i'm relatively new r, , don't have background in programming, advice appreciated.
see ?substr
.
within(df, { fact1 <- substr(factor, 1, 1) fact2 <- substr(factor, 2, 2) fact3 <- substr(factor, 4, 999) })
i'm making assumptions here how many characters use each new variable. more generality, should have kind of regular structure in factor
, eg dots separators variables.
Comments
Post a Comment