A: How to turn date only year (four digits) into a data frame?

Asked

Viewed 1,927 times

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 years even. I tried some solutions I’ve seen around, but I haven’t yet. I’m a beginner in R and I’m finding this part of turning into very difficult dates. Any tips? Grateful!

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 
  • 1

    You want to do this here: >today <- Sys.Date() >format(today, format = "%Y") [1] "2018"

  • So in R if you try to generate type Date, setting only the year it will pick a random date. For this you have to use the format that is right.

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 as. Date(date, "%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);

They have these two great sites in R:

https://www.statmethods.net/input/dates.html

https://www.r-bloggers.com/date-formats-in-r/

  • Thank you very much alxwca! The tips were valuable. It is the solution that came closer than I wanted. But when I use "format", the data changes from "Date" to "Chr" again. This did not happen with vc?

  • Yes, but since you only want to work with Year this will happen. Unless you have a library that I do not know

  • What is the purpose of manipulating the Year, do you want to add, or only to list?

  • I want to plot a time series chart. Isn’t it necessary to have the date column as "Date"? I was wondering if.

  • You understood to have Date field, you have to have the full date day, month and year, when you only handle the Year, enter format();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.