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

Rui Barradas ruipbarradas at sapo.pt
Mon Jun 19 22:05:39 CEST 2017


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:
> 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:
>
>> 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.
>>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> 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