Converting a yy-mm character into date format

Hi, I am trying to covert an csv file with date character into date in r but the date variable still remains a character as below: Any help with this. Thanks.

setwd("D:/R_sch")
Data<- read.csv("D:/R_sch/data.csv", header=T, stringsAsFactors = FALSE)
str(Data)
'data.frame': 24 obs. of 2 variables:
Time : chr "Jan-81" "Feb-81" "Mar-81" "Apr-81" ... Rainfall: int 45 67 209 180 131 115 104 184 133 121 ...
as.Date(paste("01-", Data, sep = ""), format = "%d-%b-%y")
[1] NA NA
str(Data)
'data.frame': 24 obs. of 2 variables:
Time : chr "Jan-81" "Feb-81" "Mar-81" "Apr-81" ... Rainfall: int 45 67 209 180 131 115 104 184 133 121 ...
Data
Time Rainfall
1 Jan-81 45
2 Feb-81 67
3 Mar-81 209
4 Apr-81 180
5 May-81 131
6 Jun-81 115
7 Jul-81 104
8 Aug-81 184
9 Sep-81 133
10 Oct-81 121
11 Nov-81 80
12 Dec-81 36
13 Jan-82 43
14 Feb-82 38
15 Mar-82 101
16 Apr-82 193
17 May-82 208
18 Jun-82 79
19 Jul-82 81
20 Aug-82 94
21 Sep-82 109
22 Oct-82 189
23 Nov-82 156
24 Dec-82 35

I think you're looking for something like this:

Date <- data.frame(stringsAsFactors = FALSE,
                   Time = c("Jan-81", "Feb-81", "Mar-81", "Apr-81", "May-81", "Jun-81",
                            "Jul-81", "Aug-81", "Sep-81", "Oct-81", "Nov-81", "Dec-81",
                            "Jan-82", "Feb-82", "Mar-82", "Apr-82", "May-82", "Jun-82",
                            "Jul-82", "Aug-82", "Sep-82", "Oct-82", "Nov-82", "Dec-82"),
                   Rainfall = c(45, 67, 209, 180, 131, 115, 104, 184, 133, 121, 80, 36,
                                43, 38, 101, 193, 208, 79, 81, 94, 109, 189, 156, 35)        )

Date_mod <- within(data = Date,
                   expr = {
                     Time = as.Date(x = paste0("01-", Time),
                                    format = "%d-%B-%y")
                   })

Date_mod
#>          Time Rainfall
#> 1  1981-01-01       45
#> 2  1981-02-01       67
#> 3  1981-03-01      209
#> 4  1981-04-01      180
#> 5  1981-05-01      131
#> 6  1981-06-01      115
#> 7  1981-07-01      104
#> 8  1981-08-01      184
#> 9  1981-09-01      133
#> 10 1981-10-01      121
#> 11 1981-11-01       80
#> 12 1981-12-01       36
#> 13 1982-01-01       43
#> 14 1982-02-01       38
#> 15 1982-03-01      101
#> 16 1982-04-01      193
#> 17 1982-05-01      208
#> 18 1982-06-01       79
#> 19 1982-07-01       81
#> 20 1982-08-01       94
#> 21 1982-09-01      109
#> 22 1982-10-01      189
#> 23 1982-11-01      156
#> 24 1982-12-01       35

Created on 2019-05-08 by the reprex package (v0.2.1)
Hope this helps.

For future threads, please share your dataset in a copy-paste friendly format.

The dput function is very handy, if you have stored the dataset in some R object.

In case you've your dataset on a spreadsheet, check out the datapasta package. Take a look at the following link:

Thanks Yanabrina with the help. I ran the code and still it returns Time as a character not date variable as below:

str(Date)
'data.frame': 24 obs. of 2 variables:
Time : chr "Jan-81" "Feb-81" "Mar-81" "Apr-81" ... Rainfall: num 45 67 209 180 131 115 104 184 133 121 ...

Are you sure? I don't see this problem, as you can find below:

Date <- data.frame(stringsAsFactors = FALSE,
                   Time = c("Jan-81", "Feb-81", "Mar-81", "Apr-81", "May-81", "Jun-81",
                            "Jul-81", "Aug-81", "Sep-81", "Oct-81", "Nov-81", "Dec-81",
                            "Jan-82", "Feb-82", "Mar-82", "Apr-82", "May-82", "Jun-82",
                            "Jul-82", "Aug-82", "Sep-82", "Oct-82", "Nov-82", "Dec-82"),
                   Rainfall = c(45, 67, 209, 180, 131, 115, 104, 184, 133, 121, 80, 36,
                                43, 38, 101, 193, 208, 79, 81, 94, 109, 189, 156, 35)        )

Date_mod <- within(data = Date,
                   expr = {
                     Time = as.Date(x = paste0("01-", Time),
                                    format = "%d-%B-%y")
                   })

str(object = Date_mod)
#> 'data.frame':    24 obs. of  2 variables:
#>  $ Time    : Date, format: "1981-01-01" "1981-02-01" ...
#>  $ Rainfall: num  45 67 209 180 131 115 104 184 133 121 ...

Check whether you're applying str on the old data frame or not.

Great thanks Yarnabrina, it has worked. Thanks a alot. I will also try out the datapasta package.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.