0

I have a vector that contains dates and times stored as a factor integer like this

"20151201 070104"

and I need to separate it at the space. I have tried:

dtsplit = as.numeric(strsplit(dtimes, " "))

but that doesn't work as its not in a char format.

How would I best do this?

Gordon
  • 73
  • 7
  • `dtsplit = as.numeric(strsplit(dtimes, " ")[[1]])` will work. `strsplit` returns a list of which you take the first element. – asachet Apr 21 '16 at 13:29
  • 2
    Your title is badly worded. You want to split a string into an integer vector. If you already had an integer vector, you would not need all this! How did you obtain this vector? If you read it from file, it would be easier to read it correctly directly. – asachet Apr 21 '16 at 13:33
  • I have it from a file , I listed the question this way as when I look at the vector in the variable explorer it is listed as an integer type – Gordon Apr 21 '16 at 13:37
  • @ Gordon Which would mean that it is already an integer vector. You seem to think that `dtimes` is a string containing "20151201 070104", but if R tells you this is an integer vector, your data may already be loaded as you want it. Try typing `dtimes` in the interactive console. – asachet Apr 21 '16 at 13:39
  • I have a vector that contains dates and time in this format "20151201 070104" in the variable explorer they are listed as class factor and type integer. I want to separate the vector by the space that is in between the date and time. – Gordon Apr 21 '16 at 13:42
  • The porblem I have is that the date and time are both in the same vector – Gordon Apr 21 '16 at 13:43
  • That is definitely what I want to work towards as I have a load of time series data I want to work with. The data is in a txt file that contains rows formatted like this: '20151201 070104;160.02;2 20151201 070105;160.02;2 20151201 070119;160.01;1 20151201 070127;160;1 20151201 070127;160.01;1 20151201 070210;160.02;2 ' – Gordon Apr 21 '16 at 13:46
  • what is `str(dtimes)`? – Sotos Apr 21 '16 at 13:46
  • 2
    What is wrong is that it is a factor. You can coerce it to string by using `as.character`. I would advise against that though. You should reload your data using the argument `StringsAsFactor=F` to prevent the strings being casted to factor. Then convert the strings into a proper date using for example the function `ymd_hms` from package `lubridate`. – asachet Apr 21 '16 at 13:47
  • 1
    OK thx my console is hanging at the moment but I will read the data in again correctly. I am then going to use chron to format the dates and time. Thanks for the help everyone – Gordon Apr 21 '16 at 13:50

1 Answers1

0

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
Community
  • 1
  • 1
Bas
  • 1,066
  • 1
  • 10
  • 28