Creating a time-stamp variable out of six columns from an csv-file

Hi,
i'm totally new to RStudio. So I hope you can help me with my problem:

I have loaded a csv-file into RStudio which includes data about the

  • year
  • month
  • day
  • hour
  • minute
  • second
    in separate columns.

Now I have to create a new column bringing all these before mentioned columns together in a time stamp.
I already installed the lubridate-package and I know that I have to use the command yms_hms(), but I'm not sure what exactly to include into the brackets in order to bring the data from the different columns together.

Maybe it isn't that difficult, but I just can't find a solution.
Thanks for your help!

Here is an example with made-up data

library(dplyr)
library(lubridate)

sample_df <- data.frame(
    year = c(2020, 2020),
    month = c(07, 08),
    day = c(22, 23),
    hour = c(10, 11),
    minute = c(01, 02),
    second = c(30, 32)
)

sample_df %>% 
    mutate(date = make_datetime(year, month, day, hour, minute, second))
#>   year month day hour minute second                date
#> 1 2020     7  22   10      1     30 2020-07-22 10:01:30
#> 2 2020     8  23   11      2     32 2020-08-23 11:02:32

Created on 2020-07-09 by the reprex package (v0.3.0)

If you need more specific help, please provide a proper REPRoducible EXample (reprex) illustrating your issue.

2 Likes

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

Thanks for the reply. :slight_smile:
Let's take the data.frame you created as an example:

sample_df <- data.frame(
year = c(2020, 2020),
month = c(07, 08),
day = c(22, 23),
hour = c(10, 11),
minute = c(01, 02),
second = c(30, 32)
)

In this data.frame, I now have to create a new additional column, which takes the information from the other six columns (year, month, day, hour, minute, second) and provides it in the following format:
2020-07-22 10:01:30

My example already does what you are describing, it is adding the "date" column at the end.