R: how to transform only the year (four digit) into a date frame?

0

I have a date frame with the column below. I would like to turn the years (with 4 digits) into dates (as.Date). The idea would be to keep only the same years. I tried some solutions that I saw there, but I still have not been able to. I'm a beginner at R and I'm finding this part of it turning into very difficult dates. Any tips? Thanks!

summary(CPU$A0252)
     1945          1961          1964          1966          1970 
        1             1             1             2             1 
     1971          1972          1973          1980          1981 
        1             1             1             1             3 
     1982          1983          1984          1986          1987 
        1             1             2             3             4 
     1988          1989          1990          1991          1992 
        1             4             6            11             5 
     1993          1994          1995          1996          1997 
       13             6            11            14            23 
     1998          1999          2000          2001          2002 
        7            12            12            20            19 
     2003          2004          2005          2006          2007 
       58            30            69           223           201 
     2008          2009      Ignorado            NA Não aplicável 
      113            71            92             5          4520 
    
asked by anonymous 16.01.2018 / 11:17

1 answer

1

Transforming your data frame:

str <- c("16/01/2018", "16/01/2019")
datas <- as.Date(str, "%d/%m/%Y")
datas

The default output is this below:

[1] "2018-01-16" "2019-01-16"

Formatting for 4 digit year:

format(datas, format = "%Y")
[1] "2018" "2019"

Forcing you to use format (), whenever I needed to manipulate in R I always used this function.

dataAno <- format(datas, format = "%Y")

And if you try to force the.Date (dataAno, "% d /% m /% Y") passing only the year, even if you put all the parameters you will get:

[1] NA NA

Whenever you need a help, command help (as.Date);

Have these two sites great references in R:

link

link

    
16.01.2018 / 13:01