Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (50.2k points)

I want to convert the t column in this dataframe (mbt2) to date. Dates from 1957-2020.

        t     temp

1    1/01/1957 36.1

2    2/01/1957 31.1

3    3/01/1957 31.7

4    4/01/1957 32.2

5    5/01/1957 32.2

6    6/01/1957 34.0

I have executed the following:

mbt2$t <- as.Date(mbt2$t , format = "%d/%m/%y")

However, it is changing every year to 2019 except 2020. Any ideas on how to fix this? 

1 Answer

0 votes
by (108k points)

In R programming, you can use the format: 

mbt2$t <- as.Date(mbt2$t , format = "%d/%m/%Y")

%y is 2-digit year, while %Y is 4-digit year. Your version reads just the first 2 digits from 1957 and that's why it returns 2019.

Browse Categories

...