简体   繁体   中英

extract month date year into new columns from char column in R

Hello all and Thank you in advance for your help. I am trying to break down a char column in a df into three separate columns for year, month as (mar) and day. The column I am extracting this from is char data type. Example: "2021-18-02 16:08:48" needs to become year/month/day as new columns with the separated data.

How would I go about doing this?

You can use tidyr::separate

library(tidyverse)
data <- data.frame(date = "2021-18-02 16:08:48")

data %>% 
  separate(date, into = c("Year","Day","Month","Time"), extra = "merge")

#   Year Day Month     Time
# 1 2021  18    02 16:08:48

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM