0

I want to return the first four characters in the 'Date' column, so I only get the year.

I currently have the 'date' column, which I can see is a character string. I want to get the 'year' column.

I have used the "sub" function to retrieve the month for each date observation, where I used the following code:

FF5_class$Month <- sub(".*-", "", FF5_class$date)

How do I amend the above code to return the first 4 characters?

 Date     Year
1975-12   1975
1976-01   1976
1976-02   1976

Thank you !

1 Answers1

1

You could use substr:

FF5_class$Month <- substr(FF5_class$date,1,4)
Waldi
  • 39,242
  • 6
  • 30
  • 78