The goal I'm trying to achieve is to take a data frame column which is a factor, create a new column for each level and populate the column with the appropriate value for that level from the original data frame.
Here is a sample. In this case, I want to create a new column for each level of the the.name
factor column, like 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 this be done?
EDIT: I have tried to achieve this using a sapply
loop with split
by the column and thenrbind
ing the results. However, I have not gotten it to work and chose not to add it into this question as it would generate noise - I'm pretty sure that method is not correct and can be considerably improved.