-1

I have data frame which looks something like this

    co_stkdate returns
1:1  03-Apr-95   1.685
1:2  04-Apr-95   1.529
1:3  05-Apr-95 

I want to convert it into

     co_stkdate returns
1:1  03-04-1995   1.685
1:2  04-04-1995   1.529
1:3  05-04-1995 

I tried this

myfiles$co_stkdate<- format(as.Date(myfiles$co_stkdate, format="%d-%M-%Y"))

But this is giving me result like this

co_stkdate returns
1:1       <NA>   1.685
1:2       <NA>   1.529
1:3       <NA>   1.338
1:4       <NA>  -0.236
1:5       <NA>  -0.250
1:6       <NA>   0.053
>

2 Answers2

2

This should make it possible to change your date to an appropriate date format for R

dates <- c("03-Apr-95", "04-Apr-95")

newFormat <- as.Date(dates, tryFormats = c("%d-%b-%y"))

[1] "1995-04-03" "1995-04-04"

Then format it in the usual way

format(newFormat, "%d-%m-%Y")

[1] "03-04-1995" "04-04-1995"
henrik_ibsen
  • 763
  • 1
  • 7
  • 16
2
co_stkdate <- c("03-Apr-95", "04-Apr-95", "05-Apr-95")

format(x = as.Date(x = co_stkdate,
                   format = "%d-%b-%y"),
       format = "%d-%m-%Y")
#> [1] "03-04-1995" "04-04-1995" "05-04-1995"

Created on 2019-05-20 by the reprex package (v0.3.0)

Hope this helps.

yarnabrina
  • 1,561
  • 1
  • 10
  • 30