Search code examples
rdatetimedata-manipulationnumerical

How to create a numerical variable from Qualterly time column - R


I have a dataset which currently looks like this:

Time    Var1 Var2 
2013 Q4 123  756
2013 Q4 657  987
2014 Q1 746  756
2014 Q1 66   999
2014 Q2 774  542

And I need to convert this categorical 'Time' variable into a numerical variable, something which may look potentially like this:

Time    Var1 Var2 n.Time
2013 Q4 123  756  1
2013 Q4 657  987  1
2014 Q1 746  756  2
2014 Q1 66   999  2
2014 Q2 774  542  3

Or something similar which gives the 'Time' column a numerical value which is proportional.

I have attempted the

df$n.Time <- as.yearqtr(df$Time)

But this just gives the same output as the 'Time' column instead of making it numerical.

Any help would be greatly appreciated


Solution

  • Would something like this work?

    df$n.Time <- as.numeric(as.factor(df$Time))