JRP
JRP

Reputation: 135

How do I dissect a string to create a new variable?

I have a sample dataframe:

ID   Code
 1   1, 1, 1
 2   1, 2, 3
 3   2, 2
 4   3
 5   2, 1

Where "Code" consists of colors coded 1, 2, 3, or a combination of those. "Code" is of character type. I want to create a new column based on the value(s) in "Code", so that I have a dataframe like this:

ID   Code       Description
 1   1, 1, 1          White
 2   1, 2, 3       Multiple           
 3   2, 2             Green
 4   3                 Blue
 5   2, 1          Multiple

The new column is coded as "Multiple" if there is more than 1 color in "Code"

Upvotes: 0

Views: 55

Answers (1)

akrun
akrun

Reputation: 887901

We create an named key/value vector, then split the 'Code' to create the expected output based on matching the 'v1'

v1 <- setNames( c('White', 'Green', 'Blue'), 1:3)
df1$Description <- unname(sapply(strsplit(df1$Code, ",\\s*"), function(x) 
      if(length(unique(x))> 1) 'Multiple' else v1[unique(x)]))
df1
#    ID    Code Description
#1  1 1, 1, 1       White
#2  2 1, 2, 3    Multiple
#3  3    2, 2       Green
#4  4       3        Blue
#5  5    2, 1    Multiple

Upvotes: 1

Related Questions