Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

combine month and day into one date column

Tags:

r

Using R, I'd like to combine into one column (date) the month nb (month) and the day nb (day) contained in two different columns and use the created column in a date format.

my data frame looks like this:

St Dep Month Day
A 2 1 1
B 2 1 1
B 2 2 1
A 5 1 1
A 7 1 1
C 2 1 1
C 5 1 1

And I want to add a column with something like "Jan-01".

like image 375
user2165907 Avatar asked Mar 14 '13 13:03

user2165907


People also ask

How do I concatenate date month and day in Excel?

Concatenate year, month and day to date with formulaSelect a blank cell to place the concatenated date, and enter formula =A2&"/"&B2&"/"&C2 into the formula bar, then press the Enter key. 2. Drag the populated cell's Fill Handle down to the cells for concatenating corresponding cells to date.


1 Answers

I assume here that by "date format" you just mean a string that looks like a date, rather than a proper date class in R.

data <- data.frame( Month=c(1,5,2), Day=c(1,2,3) )
data$MonthDay <- paste( month.abb[data$Month], data$Day, sep="-" )
data
#   Month Day MonthDay
# 1     1   1    Jan-1
# 2     5   2    May-2
# 3     2   3    Feb-3
like image 165
ndoogan Avatar answered Oct 28 '22 13:10

ndoogan