Joep_S
Joep_S

Reputation: 537

Dataframe: Unique values from colums into new column names

I have the following example data frame:

data.frame(ID=c(1:9),COLOR=c('red','green','blue','white','black','yellow','red','blue','red'),
           SHAPE=c('square','circle','triangle','square','triangle','circle','circle','circle','square'), 
           VAR_X=c(5,8,3,9,7,4,2,9,12))


  ID  COLOR    SHAPE VAR_X
1  1    red   square     5
2  2  green   circle     8
3  3   blue triangle     3
4  4  white   square     9
5  5  black triangle     7
6  6 yellow   circle     4
7  7    red   circle     2
8  8   blue   circle     9
9  9    red   square    12

With this dataframe I want to create a new column for each unique value from the combination of COLOR and SHAPE. So, in my example I would like the new columns: 'red square','green circle','blue triangle','white square','black triangle','yellow circle','red circle','blue circle'. The value for each observation in the new column should correspond with the COLOR and SHAPE combinations. For 'red square' this would look like:

  ID  COLOR    SHAPE VAR_X red_square
1  1    red   square     5       true
2  2  green   circle     8      false
3  3   blue triangle     3      false
4  4  white   square     9      false
5  5  black triangle     7      false
6  6 yellow   circle     4      false
7  7    red   circle     2      false
8  8   blue   circle     9      false
9  9    red   square    12       true

Since I only want the unique combinations, a new column for 'red square' should only be created once. For all possible combinations that do not exist in the dataframe (e.g. white circle) no column name should be created.

Anyone got the solution I'm looking for?

Upvotes: 2

Views: 258

Answers (1)

akrun
akrun

Reputation: 887691

We can use table

cbind(df1, as.data.frame.matrix(with(df1, table(ID, paste(COLOR, SHAPE, sep="_"))!=0)))
# ID  COLOR    SHAPE VAR_X black_triangle blue_circle blue_triangle green_circle red_circle red_square white_square yellow_circle
#1  1    red   square     5          FALSE       FALSE         FALSE        FALSE      FALSE       TRUE        FALSE         FALSE
#2  2  green   circle     8          FALSE       FALSE         FALSE         TRUE      FALSE      FALSE        FALSE         FALSE
#3  3   blue triangle     3          FALSE       FALSE          TRUE        FALSE      FALSE      FALSE        FALSE         FALSE
#4  4  white   square     9          FALSE       FALSE         FALSE        FALSE      FALSE      FALSE         TRUE         FALSE
#5  5  black triangle     7           TRUE       FALSE         FALSE        FALSE      FALSE      FALSE        FALSE         FALSE
#6  6 yellow   circle     4          FALSE       FALSE         FALSE        FALSE      FALSE      FALSE        FALSE          TRUE
#7  7    red   circle     2          FALSE       FALSE         FALSE        FALSE       TRUE      FALSE        FALSE         FALSE
#8  8   blue   circle     9          FALSE        TRUE         FALSE        FALSE      FALSE      FALSE        FALSE         FALSE
#9  9    red   square    12          FALSE       FALSE         FALSE        FALSE      FALSE       TRUE        FALSE         FALSE

Upvotes: 3

Related Questions