Skip to content
Prev 369868 / 398503 Next

How to Transform a Factor Variable into a Date

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: