combine month and day into one date column

后端 未结 2 503
萌比男神i
萌比男神i 2021-02-08 04:37

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.

2条回答
  •  鱼传尺愫
    2021-02-08 05:19

    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
    

提交回复
热议问题