How to extract year, month, day and time in 4 different columns from one column

I have a dataset consisting of a type column

Year
1/1/1996 9:00
1/2/1996 9:00
1/3/1996 9:00
1/4/1996 9:00
1/5/1996 9:00
1/6/1996 9:00
1/7/1996 9:00
1/8/1996 9:00
1/9/1996 9:00
1/10/1996 9:00

I want to create 4 different columns that will look something like this.

Year            Month   Day   Year  Time
1/1/1996 9:00   1       1     1996  9:00
1/2/1996 9:00   1       2     1996  9:00 
1/3/1996 9:00   1       3     1996  9:00 
1/4/1996 9:00   1       4     1996  9:00
1/5/1996 9:00   1       5     1996  9:00
1/6/1996 9:00   1       6     1996  9:00
1/7/1996 9:00   1       7     1996  9:00
1/8/1996 9:00   1       8     1996  9:00
1/9/1996 9:00   1       9     1996  9:00
1/10/1996 9:00  1       10    1996  9:00

Can this be done in R?

+4
source share
2 answers

We can use separateout library(tidyr). We indicate the new variables that should be created in into. Other arguments include sep, to indicate the delimiter, removeto return the original column or not, and change the column class of the new variables to type.convert=TRUE.

library(tidyr)
separate(df1, Year, into=c('Month', 'Day', 'Year', 'Time'),
                      sep='[/ ]', remove=FALSE, type.convert=TRUE)
#             Year Month Day Year Time
#1   1/1/1996 9:00     1   1 1996 9:00
#2   1/2/1996 9:00     1   2 1996 9:00
#3   1/3/1996 9:00     1   3 1996 9:00
#4   1/4/1996 9:00     1   4 1996 9:00
#5   1/5/1996 9:00     1   5 1996 9:00
#6   1/6/1996 9:00     1   6 1996 9:00
#7   1/7/1996 9:00     1   7 1996 9:00
#8   1/8/1996 9:00     1   8 1996 9:00
#9   1/9/1996 9:00     1   9 1996 9:00
#10 1/10/1996 9:00     1  10 1996 9:00

tstrsplit data.table. "data.frame" "data.table" (setDT(df1)), "" (tstrsplit(Year, ...)) (:=) .

library(data.table)#v1.9.6+
setDT(df1)[, c('Month', 'Day', 'year', 'Time') := tstrsplit(Year, '[/ ]',
                    type.convert=TRUE)]

cSplit library(splitstackshape).

library(splitstackshape)
cSplit(df1, 'Year', '[/ ]', fixed=FALSE, drop=FALSE, type.convert=TRUE)

df1 <- structure(list(Year = c("1/1/1996 9:00", "1/2/1996 9:00",
"1/3/1996 9:00", 
"1/4/1996 9:00", "1/5/1996 9:00", "1/6/1996 9:00", "1/7/1996 9:00", 
"1/8/1996 9:00", "1/9/1996 9:00", "1/10/1996 9:00")), .Names = "Year", 
class = "data.frame", row.names = c(NA, -10L))
+3

.

library(readr)
library(dplyr)
library(lubridate)
library(stringi)
df <- read_tsv("Year
            1/1/1996 9:00
            1/2/1996 9:00
            1/3/1996 9:00
            1/4/1996 9:00
            1/5/1996 9:00
            1/6/1996 9:00
            1/7/1996 9:00
            1/8/1996 9:00
            1/9/1996 9:00
            1/10/1996 9:00", 
            col_types = list(Year = col_datetime(format = "%d/%m/%Y %H:%M")))

mutate_each(df, 
            funs(month, day, year, hour, stri_datetime_format(time = Year,     
            format = "HH:mm", tz = "UTC")), 
            Year)
+1

Source: https://habr.com/ru/post/1608766/


All Articles