[R] How to Transform a Factor Variable into a Date

David L Carlson dcarlson at tamu.edu
Mon Jun 19 22:06:00 CEST 2017


The R list does not recognize .csv files so your attachment got stripped. It is picky that way. Tacking .txt to the end might trick it into preserving the attachment.

Dataset <- read.table()

created a data.frame called Dataset so the second command was redundant. Probably better would be read.csv() which automatically uses "," as the separator. Also including the argument as.is=TRUE will prevent conversion of character fields to factors. Then your conversion to dates should work once you add a day to the month-year format (e.g. first of the month or middle of the month):

> Test <- read.csv(text='"TransitDate", "CargoTons"
+ "Apr-2013", 50
+ "Jun-2013", 40
+ "Jul-2013", 30', as.is=TRUE)
> Test$TransitDate <- as.Date(paste0("01-", Test$TransitDate), "%d-%b-%Y")
> str(Test)
'data.frame':   3 obs. of  2 variables:
 $ TransitDate: Date, format: "2013-04-01" "2013-06-01" ...
 $ CargoTons  : int  50 40 30
> Test
  TransitDate CargoTons
1  2013-04-01        50
2  2013-06-01        40
3  2013-07-01        30

-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352



-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Paul Bernal
Sent: Monday, June 19, 2017 2:07 PM
To: r-help at r-project.org
Subject: [R] How to Transform a Factor Variable into a Date

Dear all,

Hope you are doing great. I have a .csv file that I read into R, the .csv
file consistss of two fields (TransitDate and CargoTons).

The TransitDate I formatted from Excel in the fashion mmm-yy (e.g.:
Apr-2013). However R does not recognize the field TransitDate as a date
field.

Here is the code:

library(lubridate)

Dataset <- read.table("U:/NEWCargoData.csv", header=TRUE, sep=",",
na.strings="NA", dec=".", strip.white=TRUE)

DatasetFrame <- data.frame(Dataset)

DatasetFrame$TransitDate <- as.Date(DatasetFrame$TransitDate, format =
"%b-%y")


Now, when I do DatasetFrame[1,1], the following happens:


> DatasetFrame[1,1]
[1] NA
>
> DatasetFrame[2,1]
[1] NA
>

Now when I do:

> Dataset[1,1] #this is the dataset as was read from my computer
[1] Jun-11
62 Levels: Apr-13 Apr-14 Apr-15 Apr-16 Apr-17 Aug-13 Aug-14 Aug-15 Aug-16
Dec-12 Dec-13 Dec-14 ... Sep-16
>

I am also attaching the .csv file for your reference. How can I do to get R
to convert TransitDate into an actual date  field? R is not recognizing it
as a date.

Any help will be greatly appreciated,

Best regards,

Paul
______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list