ca_san
ca_san

Reputation: 193

Why cant R vectors containing strings be replaced with numbers?

I have a vector within an R dataframe wich literally contains an abbreviation for the months in a year in the form (JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC) and I want to replace them for their traditional equivalent [1:12]

came up with the following ideas, all of which give a vector filled with not available (NA) values.

replace(df$month, df$month == 'JAN', '01')

df$month <- if(df$month == "JAN") '01'

df$month <- match(df$month,month.abb) 

the first two only make NA values were JAN was, the third one makes all months NA values

Any ideas why this isn't working, and how to get it to work?

Upvotes: 0

Views: 127

Answers (1)

Benjamin
Benjamin

Reputation: 17369

I'd be inclined to do this with merge.

MonthRef <- data.frame(month_number = 1:12,
                       month_abb = toupper(month.abb))
#* Make a data frame of random months
Months <- data.frame(month = sample(MonthRef$month_abb, 20, replace=TRUE))

merge(Months, MonthRef, by.x="month", by.y="month_abb")

It's a bit more typing, but it has the advantage that it will be very clear to me what I did when I come back to it in six months.

Upvotes: 1

Related Questions