简体   繁体   中英

Creating dataframe in R with multiple lines of the same hour

I'm trying to create a dataframe with the following columns: dt, depth, var1

But I need 4 lines of each hour going through a whole year as I need to adjust var1 at certain depths:

dt depth var1
2008-01-01 00:00 2 0.01
2008-01-01 00:00 40 0.01
2008-01-01 00:00 45 0.01
2008-01-01 00:00 100 0.01
2008-01-01 01:00 2 0.01
2008-01-01 01:00 40 0.01
2008-01-01 01:00 45 0.01
2008-01-01 01:00 100 0.01
2008-01-01 02:00 2 0.01
2008-01-01 02:00 40 0.01
2008-01-01 02:00 45 0.01
2008-01-01 02:00 100 0.01
2008-01-01 03:00 2 0.01

How do I create the "dt" list for the first column?

Thank you!

You can use expand.grid :

expand.grid(
  dt = seq(as.POSIXct("2008-01-01 00:00:00", 'UTC'), 
           as.POSIXct("2008-01-01 03:00:00", 'UTC'), 'hour'), 
  depth = c(2, 40, 45, 100), 
  var1 = 0.01
) -> result

result

#                    dt depth var1
#1  2008-01-01 00:00:00     2 0.01
#2  2008-01-01 01:00:00     2 0.01
#3  2008-01-01 02:00:00     2 0.01
#4  2008-01-01 03:00:00     2 0.01
#5  2008-01-01 00:00:00    40 0.01
#6  2008-01-01 01:00:00    40 0.01
#7  2008-01-01 02:00:00    40 0.01
#8  2008-01-01 03:00:00    40 0.01
#9  2008-01-01 00:00:00    45 0.01
#10 2008-01-01 01:00:00    45 0.01
#11 2008-01-01 02:00:00    45 0.01
#12 2008-01-01 03:00:00    45 0.01
#13 2008-01-01 00:00:00   100 0.01
#14 2008-01-01 01:00:00   100 0.01
#15 2008-01-01 02:00:00   100 0.01
#16 2008-01-01 03:00:00   100 0.01

If you want order as shown you can arrange the above result or use tidyr::expand_grid :

tidyr::expand_grid(
  dt = seq(as.POSIXct("2008-01-01 00:00:00", 'UTC'), 
           as.POSIXct("2008-01-01 03:00:00", 'UTC'), 'hour'), 
  depth = c(2, 40, 45, 100), 
  var1 = 0.01
) -> result

result

# A tibble: 16 x 3
#   dt                  depth  var1
#   <dttm>              <dbl> <dbl>
# 1 2008-01-01 00:00:00     2  0.01
# 2 2008-01-01 00:00:00    40  0.01
# 3 2008-01-01 00:00:00    45  0.01
# 4 2008-01-01 00:00:00   100  0.01
# 5 2008-01-01 01:00:00     2  0.01
# 6 2008-01-01 01:00:00    40  0.01
# 7 2008-01-01 01:00:00    45  0.01
# 8 2008-01-01 01:00:00   100  0.01
# 9 2008-01-01 02:00:00     2  0.01
#10 2008-01-01 02:00:00    40  0.01
#11 2008-01-01 02:00:00    45  0.01
#12 2008-01-01 02:00:00   100  0.01
#13 2008-01-01 03:00:00     2  0.01
#14 2008-01-01 03:00:00    40  0.01
#15 2008-01-01 03:00:00    45  0.01
#16 2008-01-01 03:00:00   100  0.01

Try this:

start_date_time <- as.POSIXct("2008-01-01 00:00", format= "%Y-%m-%d %H:%M")
end_date_time <- as.POSIXct("2008-01-01 03:00", format= "%Y-%m-%d %H:%M")

df <- data.frame(dt = rep(seq(start_date_time, end_date_time, by = 3600), each = 4), 
                 depth = rep(c(2, 40, 45, 100)),
                 var1 = 0.01)

df
#>                     dt depth var1
#> 1  2008-01-01 00:00:00     2 0.01
#> 2  2008-01-01 00:00:00    40 0.01
#> 3  2008-01-01 00:00:00    45 0.01
#> 4  2008-01-01 00:00:00   100 0.01
#> 5  2008-01-01 01:00:00     2 0.01
#> 6  2008-01-01 01:00:00    40 0.01
#> 7  2008-01-01 01:00:00    45 0.01
#> 8  2008-01-01 01:00:00   100 0.01
#> 9  2008-01-01 02:00:00     2 0.01
#> 10 2008-01-01 02:00:00    40 0.01
#> 11 2008-01-01 02:00:00    45 0.01
#> 12 2008-01-01 02:00:00   100 0.01
#> 13 2008-01-01 03:00:00     2 0.01
#> 14 2008-01-01 03:00:00    40 0.01
#> 15 2008-01-01 03:00:00    45 0.01
#> 16 2008-01-01 03:00:00   100 0.01

Created on 2021-04-22 by the reprex package (v2.0.0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM