My dataframe looks like this:
dput(head(sAUR,50))
structure(list(date = structure(c(1475315922, 1475660422, 1475920222,
1476179992, 1476524422, 1476784232, 1477044062, 1477388482, 1477648302,
1477911722, 1478256142, 1478515962, 1478775782, 1479120192, 1479380002,
1479639822, 1479984232, 1480244042, 1480503862, 1480848262, 1481108072,
1481367882, 1481712272, 1481972082, 1482231892, 1482576282, 1482836082,
1483095882, 1483440272, 1484304241, 1484564041, 1484823831, 1485428001,
1485687791, 1486032161, 1486291941, 1486551731, 1486896101, 1487155881,
1487415671, 1487760031, 1488019821, 1488279601, 1488624021, 1488883821,
1489143521, 1489488011, 1489747821, 1490007451, 1490352021), class = c("POSIXct",
"POSIXt"), tzone = ""), layer = c("AUR", "AUR", "AUR", "AUR",
"AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "AUR",
"AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "AUR",
"AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "AUR",
"AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "AUR",
"AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "AUR",
"AUR")), row.names = c(NA, 50L), class = "data.frame")
I want to copy the rows from 2017-02-18 11:01:11 (1487415671) until 2017-03-20 10:57:31 (1490007451) and paste them to the top of the dataframe while keeping the original data, in other words, I want to add this chunk of rows on top of the original dataframe.
(if there's a solution using the index I would consider it as well)
Any help will be much appreciated.
CodePudding user response:
Option using rbind
:
sAUR_copy <- subset(sAUR, date %in% 1487415671:1490007451)
rbind(sAUR_copy, sAUR)
#> date layer
#> 40 2017-02-18 12:01:11 AUR
#> 41 2017-02-22 11:40:31 AUR
#> 42 2017-02-25 11:50:21 AUR
#> 43 2017-02-28 12:00:01 AUR
#> 44 2017-03-04 11:40:21 AUR
#> 45 2017-03-07 11:50:21 AUR
#> 46 2017-03-10 11:58:41 AUR
#> 47 2017-03-14 11:40:11 AUR
#> 48 2017-03-17 11:50:21 AUR
#> 49 2017-03-20 11:57:31 AUR
#> 1 2016-10-01 11:58:42 AUR
#> 2 2016-10-05 11:40:22 AUR
#> 3 2016-10-08 11:50:22 AUR
#> 4 2016-10-11 11:59:52 AUR
#> 5 2016-10-15 11:40:22 AUR
#> 6 2016-10-18 11:50:32 AUR
#> 7 2016-10-21 12:01:02 AUR
#> 8 2016-10-25 11:41:22 AUR
#> 9 2016-10-28 11:51:42 AUR
#> 10 2016-10-31 12:02:02 AUR
#> 11 2016-11-04 11:42:22 AUR
#> 12 2016-11-07 11:52:42 AUR
#> 13 2016-11-10 12:03:02 AUR
#> 14 2016-11-14 11:43:12 AUR
#> 15 2016-11-17 11:53:22 AUR
#> 16 2016-11-20 12:03:42 AUR
#> 17 2016-11-24 11:43:52 AUR
#> 18 2016-11-27 11:54:02 AUR
#> 19 2016-11-30 12:04:22 AUR
#> 20 2016-12-04 11:44:22 AUR
#> 21 2016-12-07 11:54:32 AUR
#> 22 2016-12-10 12:04:42 AUR
#> 23 2016-12-14 11:44:32 AUR
#> 24 2016-12-17 11:54:42 AUR
#> 25 2016-12-20 12:04:52 AUR
#> 26 2016-12-24 11:44:42 AUR
#> 27 2016-12-27 11:54:42 AUR
#> 28 2016-12-30 12:04:42 AUR
#> 29 2017-01-03 11:44:32 AUR
#> 30 2017-01-13 11:44:01 AUR
#> 31 2017-01-16 11:54:01 AUR
#> 32 2017-01-19 12:03:51 AUR
#> 33 2017-01-26 11:53:21 AUR
#> 34 2017-01-29 12:03:11 AUR
#> 35 2017-02-02 11:42:41 AUR
#> 36 2017-02-05 11:52:21 AUR
#> 37 2017-02-08 12:02:11 AUR
#> 38 2017-02-12 11:41:41 AUR
#> 39 2017-02-15 11:51:21 AUR
#> 401 2017-02-18 12:01:11 AUR
#> 411 2017-02-22 11:40:31 AUR
#> 421 2017-02-25 11:50:21 AUR
#> 431 2017-02-28 12:00:01 AUR
#> 441 2017-03-04 11:40:21 AUR
#> 451 2017-03-07 11:50:21 AUR
#> 461 2017-03-10 11:58:41 AUR
#> 471 2017-03-14 11:40:11 AUR
#> 481 2017-03-17 11:50:21 AUR
#> 491 2017-03-20 11:57:31 AUR
#> 50 2017-03-24 11:40:21 AUR
Created on 2022-07-06 by the reprex package (v2.0.1)
CodePudding user response:
Tidyverse approach:
library(tibble)
library(dplyr)
df <- structure(list(
date = structure(
c(1475315922, 1475660422, 1475920222,
1476179992, 1476524422, 1476784232, 1477044062, 1477388482, 1477648302,
1477911722, 1478256142, 1478515962, 1478775782, 1479120192, 1479380002,
1479639822, 1479984232, 1480244042, 1480503862, 1480848262, 1481108072,
1481367882, 1481712272, 1481972082, 1482231892, 1482576282, 1482836082,
1483095882, 1483440272, 1484304241, 1484564041, 1484823831, 1485428001,
1485687791, 1486032161, 1486291941, 1486551731, 1486896101, 1487155881,
1487415671, 1487760031, 1488019821, 1488279601, 1488624021, 1488883821,
1489143521, 1489488011, 1489747821, 1490007451, 1490352021),
class = c("POSIXct", "POSIXt"), tzone = ""),
layer = c("AUR", "AUR", "AUR", "AUR",
"AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "AUR",
"AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "AUR",
"AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "AUR",
"AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "AUR",
"AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "AUR", "AUR",
"AUR")),
row.names = c(NA, 50L),
class = "data.frame"
)
new_df <- df %>%
as_tibble() %>%
filter(between(date, 1487415671, 1490007451)) %>%
bind_rows(df)
new_df
#> # A tibble: 60 × 2
#> date layer
#> <dttm> <chr>
#> 1 2017-02-18 17:01:11 AUR
#> 2 2017-02-22 16:40:31 AUR
#> 3 2017-02-25 16:50:21 AUR
#> 4 2017-02-28 17:00:01 AUR
#> 5 2017-03-04 16:40:21 AUR
#> 6 2017-03-07 16:50:21 AUR
#> 7 2017-03-10 16:58:41 AUR
#> 8 2017-03-14 16:40:11 AUR
#> 9 2017-03-17 16:50:21 AUR
#> 10 2017-03-20 16:57:31 AUR
#> # … with 50 more rows
Created on 2022-07-06 by the reprex package (v2.0.1)
CodePudding user response:
You could create a new dataframe of the rows you'd like to have at top, lets call it df1:
library(dplyr)
df1 = df %>% filter(date>1487415671, date<1490007451)
Assuming df is your original dataset. And removing thoes rows from your df:
df = df%>% filter(!(date>1487415671, date<1490007451))
And then bind them together again:
df = bind_rows(df1,df)
CodePudding user response:
You can do in base
R:
rg <- c(as.POSIXct("2017-02-18 11:01:11"), as.POSIXct("2017-03-20 10:57:31"))
pos <- df$date >= rg[1] & df$date <= rg[2]
rbind.data.frame(df[pos, ], df[!pos, ])
Please take care of timezone argument in as.POSIXct