JRBatHolmes
JRBatHolmes

Reputation: 88

Group levels of an existing factor

A variable in my dataset looks like this

df <- data.frame(Month = factor(c(sample(1:12, 15, replace = T), 
                                  sample(c("Apr", "May"), 5, replace = T))))

Now, levels Apr & May were entered later in time by a different person, thereby stored as name of the month. So how do I get rid of the separate levels and group those values under the already existing 4 & 5 levels respectively? or conversely, how do store all values in names of months instead of numbers?

Upvotes: 0

Views: 20

Answers (1)

Sotos
Sotos

Reputation: 51582

You can match with month.abb, i.e.

i1 <- match(df$Month, month.abb)
df$Month[!is.na(i1)] <- i1[!is.na(i1)]
df
#   Month
#1      5
#2      2
#3      7
#4     12
#5      5
#6     12
#7      4
#8      6
#9      7
#10    10
#11     9
#12     4
#13    11
#14    10
#15     3
#16     4
#17     5
#18     4
#19     4
#20     4

Upvotes: 2

Related Questions