如果您需要将月份设置为(例如)"Jul"
,则只需:
do.call(rbind, strsplit(vec, "[, ]+"))
# [,1] [,2] [,3]
# [1,] "Jul" "1" "2004"
# [2,] "Jul" "10" "2018"
# [3,] "Jul" "16" "2014"
# [4,] "Aug" "30" "2015"
# [5,] "Aug" "3" "2017"
# [6,] "Jul" "1" "2014"
# [7,] "Jul" "1" "2018"
# [8,] "Jul" "19" "2018"
# [9,] "Jul" "1" "2017"
# [10,] "Jul" "1" "2016"
# [11,] "Jan" "1" "2018"
# [12,] "Jul" "1" "2014"
# [13,] "Jul" "11" "2013"
# [14,] "Aug" "9" "2018"
# [15,] "Jul" "1" "2011"
# [16,] "Jul" "9" "2009"
# [17,] "Aug" "1" "2005"
# [18,] "Jul" "28" "2011"
# [19,] "Jul" "16" "2016"
# [20,] "Jul" "1" "2015"
如果你需要它作为一个月数,那么
as.Date(vec, format = "%b %d, %Y") |>
as.character() |>
strsplit("-") |>
do.call(rbind, args=_)
# [,1] [,2] [,3]
# [1,] "2004" "07" "01"
# [2,] "2018" "07" "10"
# [3,] "2014" "07" "16"
# [4,] "2015" "08" "30"
# [5,] "2017" "08" "03"
# [6,] "2014" "07" "01"
# [7,] "2018" "07" "01"
# [8,] "2018" "07" "19"
# [9,] "2017" "07" "01"
# [10,] "2016" "07" "01"
# [11,] "2018" "01" "01"
# [12,] "2014" "07" "01"
# [13,] "2013" "07" "11"
# [14,] "2018" "08" "09"
# [15,] "2011" "07" "01"
# [16,] "2009" "07" "09"
# [17,] "2005" "08" "01"
# [18,] "2011" "07" "28"
# [19,] "2016" "07" "16"
# [20,] "2015" "07" "01"
它们都返回包含三列的matrix
,您可以转换为data.frame
并使用类似于以下内容的名称来命名这些列:
... |>
as.data.frame() |>
setNames(c("Year", "Month", "Day"))
如果需要,cbind
或以其他方式指定给当前帧.