Marco
Marco

Reputation: 4290

How to convert a column in a data.frame from POSIXct to date in R

I am reading data from an excel file and in one column I have dates which are read as POSIXct in the data.frame.

Here the content of this column:

>RawData_Date<-read_excel(path=excel_file,range=rangedate,
        col_types="date", col_names= FALSE)
>str(RawData_Date)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   1000 obs. of  1 variable:
$ X__1: POSIXct, format: "1963-01-01" "1963-01-02" "1963-01-03" "1963-01-04" ...

Later on in my code I got a date-time value "1963-01-01 01:00:00" which might be due to the manipulation, but I do not need and do not care about the time part, so I would like to convert the whole column to a date type as early as possible after reading the excel file.

Which is the most elegant way to do so?

Upvotes: 3

Views: 8617

Answers (1)

Jacqueline Nolis
Jacqueline Nolis

Reputation: 1547

Use the as.Date function. In this case your column is named "X__1", therefore:

RawData_Date$X__1 <- as.Date(RawData_Date$X__1, format = "%Y-%m-%d")

Upvotes: 7

Related Questions