Sourabh Banik
Sourabh Banik

Reputation: 53

How to substitute multiple alphanumeric characters in R?

In a dataframe DF which is as follows:

DF$Values

Values
B1
A
A2
B
B2
A1
C2
C1
C

I want to substitute A2,B,B2 as Group1, B1,C2,C1,C as Group2, and A1, A as Group3 and so on. Such that the output data looks like this

Values
Group2
Group3
Group1
Group1
Group1
Group3
Group2
Group2
Group2

I tried using gsub() but it is not working properly for me, as the code gsub("A1|A", "Group3" ,DF) replaces A2 with Group32

Please help!

Upvotes: 0

Views: 39

Answers (2)

Ronak Shah
Ronak Shah

Reputation: 388797

How about creating factor levels for each group?

df$Values <- as.factor(df$Values)
levels(df$Values) <- list(Group1= c("A2","B","B2"), Group2= c("B1","C2","C1","C"),
                                                    Group3 = c("A1", "A"))
df$Values
#[1] Group2 Group3 Group1 Group1 Group1 Group3 Group2 Group2 Group2
#Levels: Group1 Group2 Group3

Upvotes: 1

akrun
akrun

Reputation: 886938

We can do an ifelse with %in% (it is not clear if there are any specific patterns from the OP's post) to recode the elements to different one.

with(DF, ifelse(Values %in% c("A2", "B", "B2"), "Group1",
       ifelse(Values %in% c("B1", "C2", "C1", "C"), "Group2", "Group3")))
#[1] "Group2" "Group3" "Group1" "Group1" "Group1" "Group3" "Group2" "Group2" "Group2"

Upvotes: 0

Related Questions