You can split your dtimes by using strspit
as you already found out. Because your string is numeric right now you first need to convert it to char, using the as.character()
function.
Our solution would be:
dtimes <- "20151201 070104"
dtsplit = strsplit(as.character(dtimes), " ")
If you have a table containing multiple dates like the one you posted in your comment:
df <- structure(list(V1 = structure(c(1L, 2L, 3L, 4L, 4L, 5L), .Label = c("20151201 070104", "20151201 070105", "20151201 070119", "20151201 070127", "20151201 070210" ), class = "factor"), V2 = c(160.02, 160.02, 160.01, 160, 160.01, 160.02), V3 = c(2, 2, 1, 1, 1, 2)), .Names = c("V1", "V2", "V3" ), class = "data.frame", row.names = c(NA, -6L))
My guess is you need the date from each column, not the time. You can use the sapply
function to apply the strsplit to each row (this question has more information about this):
df$date <- sapply(strsplit(as.character(df$V1), " ")[], "[[", 1)
df$time <- sapply(strsplit(as.character(df$V1), " ")[], "[[", 2)
#> df
# V1 V2 V3 date time
#1 20151201 070104 160.02 2 20151201 070104
#2 20151201 070105 160.02 2 20151201 070105
#3 20151201 070119 160.01 1 20151201 070119
#4 20151201 070127 160.00 1 20151201 070127
#5 20151201 070127 160.01 1 20151201 070127
#6 20151201 070210 160.02 2 20151201 070210