0

I have been learning R through a series of videos on Google Developers youtube channel.

But I got stuck on https://www.youtube.com/watch?v=cx_3zWo4sUs&list=PLOU2XLYxmsIK9qQfztXeybpHvru-TrqAP&index=9

Particularly the transformation of the factor into date. It always ends up as NA.

The code I tried is the same as in the video, and I also tried first translating it to char. But nothing works.

stocks <- read.delim("stocks.txt")

str(stocks)

summary(stocks)

head(stocks$date, 20)

txt.date <- as.character(stocks$date)
txt.date
head(txt.date, 10)

s.date <- as.Date(txt.date, "%d-%b-%y")
s.date

s.date <- as.Date(stocks$date, format = "%d-%b-%y")
s.date

The file stocks.txt is available at http://rfunction.com/archives/2859 if needed.

I expect I am just doing something stupid as I don't yet know the language, but I could use some help.

TineO
  • 1,023
  • 8
  • 24
  • Hey, have you tried the solutions proposed here? https://stackoverflow.com/questions/17496358/r-help-converting-factor-to-date You may also specify your data types directly when using read.delim (?read.delim see colClasses). Or avoid creating factors by setting stringsAsFactors = FALSE in your read.delim call – Arcoutte Aug 14 '19 at 12:08
  • this works for me : `stocks$date <- as.Date(stocks$date, "%d-%b-%y")` – Ronak Shah Aug 14 '19 at 12:09
  • @Arcoutte I have tried those solutions, except the one with the lubridate package as there has got to be a way to do this w/o the package. I ran the code I pasted below and it didnt work mydate <- factor("20-Mar-13") mydate as.Date(mydate, format = "%d-%b-%y") – TineO Aug 14 '19 at 12:14
  • @RonakShah tried it, but they all went NA – TineO Aug 14 '19 at 12:16
  • 1
    What is your locale? `%b` is locale-dependent. If it is other than English you need to set it to English first for this to work. `Sys.setlocale("LC_ALL","English")` – Ronak Shah Aug 14 '19 at 12:19
  • @RonakShah Thank you, this was it, the locale was different. Thank you – TineO Aug 14 '19 at 12:21

0 Answers0