-1

I want to split a 'timestamp' column in my dataset into two separate one, the dataset is very lengthy

enter image description here

I expect two different columns, one for date '2014-09-22'and other for time '07:47:00'

M--
  • 25,431
  • 8
  • 61
  • 93
  • Since R does not have a time object only a date and datetime object, thus I would not recommend splitting the column in two. Of course this all depends on what additional analysis you intend. – Dave2e Nov 07 '19 at 18:15
  • Thank you @Dave2e! I want to sort the data by individual year but I have problem in sorting it successfully. If i use the data I have and sort, either the variables or the rows are missing in the resulting dataset – chandan K B Nov 07 '19 at 18:22
  • R doesn't, but lubridate, I believe, does. – iod Nov 07 '19 at 18:23
  • Why did you tag this question with `python` if you want a solution in the `R` language? – Toothpick Anemone Nov 07 '19 at 18:26
  • what's the class of your timestamp? Are these just strings, or is it something like POSIXct/lt? – iod Nov 07 '19 at 18:34
  • @chandanKB, "I want to sort the data by individual year but I have problem in sorting it successfully." This is a better question than one posted. Provide some sample data and the code you tried, you will have better success. See this for tips: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Dave2e Nov 07 '19 at 18:39
  • @SamuelMuldoon if you there is a solution for this in python that is also helpful. – chandan K B Nov 08 '19 at 13:03

2 Answers2

0

You can use the strsplit function and split on a space. That's implying your data has no spaces after which it doesn't seem so.

df$nwcolumn <- strsplit(df$timestamp, " ")
Xderic
  • 25
  • 5
  • Can you call str(df) on your df? or say what class is the timestamp? – Xderic Nov 07 '19 at 18:39
  • `strsplit` will split one element (string) at a time, but here `strsplit(df$timestamp, " ")` we expect `strsplit` process whole column, which is not the `strsplit` was designed to do. The only way to use this code is to pass it into a loop and process one element (string) at a time – Bill Chen Nov 07 '19 at 18:46
  • @Xderic the class of timestamp is a factor – chandan K B Nov 11 '19 at 11:43
0

Try this:

library(tidyr)
df <- data.frame(x = c("2014-09-32 07:47:00", "2016-09-13 14:32:00", "2017-09-18 12:42:00"))
df %>% separate(x, c("A", "B"), sep = " ")

Output is:

A           B
<chr>       <chr>
2014-09-32  07:47:00        
2016-09-13  14:32:00        
2017-09-18  12:42:00
Bill Chen
  • 1,699
  • 14
  • 24