Skip to content

How to Transform a Factor Variable into a Date

4 messages · Paul Bernal, Christopher W. Ryan, Rui Barradas +1 more

#
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:
[1] NA
[1] NA
Now when I do:
[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
#
A couple thoughts:

1. converting factors into dates often requires that they be converted to
character first.

2. you don't really have dates; you have just months and years

3. therefore perhaps the as.yearmon() function in the zoo package could help

library(zoo)
my.factor <- factor("Feb 2017")
as.yearmon(my.factor)  ## gets around the factor-vs-character issue
On Mon, Jun 19, 2017 at 3:07 PM, Paul Bernal <paulbernal07 at gmail.com> wrote:

            

  
  
#
Hello,

Another way of getting dates, of class 'Date', is to paste a day "01" 
into what the op has.
To the op:
1) Your attachment didn't come through, R-Help doesn't accept the 
extension .csv, use .txt
2) When you read your data in using function read.csv the result already 
is a data.frame so the instruction

DatasetFrame <- data.frame(Dataset)

is not needed.
3) You can read the data and create a data.frame without the problem of 
getting factors instead of characters, just set option

stringsAsFactors = FALSE

in your calls to read.*

And now some code. Note that I've made up some data, since we don't have 
the .csv file.


x <- "Jun-11"
Dataset <- data.frame(x)
str(Dataset)

tmp <- paste("01", as.character(Dataset$x), sep = "-")
Dataset$x <- as.Date(tmp, format = "%d-%b-%y")
str(Dataset)


Hope this helps,

Rui Barradas

Em 19-06-2017 20:30, Christopher W Ryan escreveu:
#
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):
+ "Apr-2013", 50
+ "Jun-2013", 40
+ "Jul-2013", 30', as.is=TRUE)
'data.frame':   3 obs. of  2 variables:
 $ TransitDate: Date, format: "2013-04-01" "2013-06-01" ...
 $ CargoTons  : int  50 40 30
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:
[1] NA
[1] NA
Now when I do:
[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.