Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to convert a data frame column to date that contains integer values in R?
If we have an integer column that actually contains date values, for example having 29th September 2020 as 20200929 then we can convert it to date by using transform function by reading the dates with as.Date function but as.character will also be needed so that as.Date function can read the date values.
Example1
S.NO<−1:20 Date<−c(19980313, 19980604, 19980409, 19980104, 19980501, 19980101, 19980521, 19980412, 19980601, 19980711, 19980615, 19980810, 19980509, 19980221, 19981101, 19981121, 19981009, 19980915, 19980929, 19980813) df2<−data.frame(S.NO,Date) df2
Output
S.NO Date 1 1 19980313 2 2 19980604 3 3 19980409 4 4 19980104 5 5 19980501 6 6 19980101 7 7 19980521 8 8 19980412 9 9 19980601 10 10 19980711 11 11 19980615 12 12 19980810 13 13 19980509 14 14 19980221 15 15 19981101 16 16 19981121 17 17 19981009 18 18 19980915 19 19 19980929 20 20 19980813
Converting Date to appropriate date format −
df2<−transform(df2,Date=as.Date(as.character(Date),"%Y%m%d")) df2
Output
S.NO Date 1 1 1998−03−13 2 2 1998−06−04 3 3 1998−04−09 4 4 1998−01−04 5 5 1998−05−01 6 6 1998−01−01 7 7 1998−05−21 8 8 1998−04−12 9 9 1998−06−01 10 10 1998−07−11 11 11 1998−06−15 12 12 1998−08−10 13 13 1998−05−09 14 14 1998−02−21 15 15 1998−11−01 16 16 1998−11−21 17 17 1998−10−09 18 18 1998−09−15 19 19 1998−09−29 20 20 1998−08−13
Advertisements
