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)
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))