How do I split a column of factors into multiple columns of multiple factors?

r

Solution

See `?substr`.

within(df, {
    Fact1 <- substr(Factor, 1, 1)
    Fact2 <- substr(Factor, 2, 2)
    Fact3 <- substr(Factor, 4, 999)
})

I'm making assumptions here about how many characters to use for each new variable. For more generality, you should have some kind of regular structure in `Factor`, eg dots as separators for all your variables.

Problem

Say I have a data frame that looks like 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 is to add columns to the data frame that extract information from the single Factor column, as such: ``` Factor Value Fact1 Fact2 Fact3 ====== ===== ===== ===== ===== 1A.In 1.0 1 A In 1A.Out 2.6 1 A Out 1B.In 0.5 1 B In 1B.Out 3.4 1 B Out 2A.In 5.5 2 A In ``` I just cannot figure out how to do this without using subset ad nauseum. I'm relatively new to R, and don't have a background in programming, so any advice would be much appreciated.

Original source