How to combine year, month, and day column in an R data frame?

R ProgrammingServer Side ProgrammingProgramming

Sometimes date variable is recorded in three different columns representing year, month, and day instead of a single column as date. Therefore, we need to combine these three columns and create a single column. This can be done by using paste function and define the values with as.Date.

Consider the below data frame −

Example

 Live Demo

Year<-sample(2001:2005,20,replace=TRUE)
Month<-sample(1:12,20,replace=TRUE)
Day<-sample(1:15,20,replace=TRUE)
df1<-data.frame(Year,Month,Day)
df1

Output

   Year  Month Day
1  2005  11    6
2  2003  7     1
3  2002  2    11
4  2001  2     5
5  2005  1     3
6  2001  7    14
7  2004  3     8
8  2004  1    10
9  2005 11    12
10 2002  3     9
11 2001 10    10
12 2001  7     8
13 2002  3     7
14 2004  8     4
15 2002  8     7
16 2005  4     7
17 2003 10     5
18 2002 12    13
19 2004  5     1
20 2003  3     8

Creating new column named as Date by combining Year, Month, and Day −

Example

df1$Date<-as.Date(with(df1,paste(Year,Month,Day,sep="-")),"%Y-%m-%d")
df1

Output

   Year  Month Day Date
1  2005  11    6   2005-11-06
2  2003  7     1  2003-07-01
3  2002  2    11  2002-02-11
4  2001  2    5   2001-02-05
5  2005  1    3   2005-01-03
6  2001  7    14  2001-07-14
7  2004  3    8   2004-03-08
8  2004  1    10  2004-01-10
9  2005  11   12  2005-11-12
10 2002  3    9   2002-03-09
11 2001  10   10  2001-10-10
12 2001  7    8   2001-07-08
13 2002  3   7    2002-03-07
14 2004  8   4    2004-08-04
15 2002  8   7    2002-08-07
16 2005  4   7    2005-04-07
17 2003 10   5    2003-10-05
18 2002  12  13   2002-12-13
19 2004  5   1    2004-05-01
20 2003  3   8    2003-03-08

Let’s have a look at another example −

Example

 Live Demo

Year<-sample(2011:2020,20,replace=TRUE) 
Month<-sample(1:12,20,replace=TRUE)
Day<-sample(1:25,20,replace=TRUE) 
df2<-data.frame(Day,Month,Year)
df2

Output

Day Month Year
1 14 3 2015
2 14 5 2013
3 5 10 2019
4 25 11 2011
5 21 7 2017
6 16 5 2019
7 10 2 2015
8 1 6 2012
9 13 8 2018
10 8 2 2019
11 18 7 2012
12 20 11 2018
13 19 12 2020
14 16 10 2015
15 9 3 2020
16 1 7 2017
17 10 12 2018
18 6 11 2014
19 9 6 2014
20 15 10 2019

Creating new column named as Date by combining Year, Month, and Day −

Example

df2$Date<-as.Date(with(df2,paste(Year,Month,Day,sep="-")),"%Y-%m-%d")
df2

Output

  Day  Month Year Date
1  14   3  2015 2015-03-14
2  14   5  2013 2013-05-14
3   5   10 2019 2019-10-05
4  25   11 2011 2011-11-25
5  21    7 2017 2017-07-21
6  16    5 2019 2019-05-16
7  10    2 2015 2015-02-10
8   1    6 2012 2012-06-01
9  13    8 2018 2018-08-13
10  8    2 2019 2019-02-08
11 18    7 2012 2012-07-18
12 20   11 2018 2018-11-20
13 19   12 2020 2020-12-19
14 16   10 2015 2015-10-16
15 9     3 2020 2020-03-09
16 1     7 2017 2017-07-01
17 10   12 2018 2018-12-10
18 6    11 2014 2014-11-06
19 9     6 2014 2014-06-09
20 15   10 2019 2019-10-15
raja
Published on 14-Oct-2020 12:42:57
Advertisements