How to remove rows with paired numbers?
Solution 1:
In base R
, use a recyling logical vector to remove the even rows, then with transform
, convert the 'V1' to Date
class and format
by adding the 01
as day
transform(dat[c(TRUE, FALSE),], V1 = format(as.Date(V1), '%Y-%m-01'))
-output
V1 V4
1 1901-01-01 12.5
3 1901-02-01 13.1
5 1901-03-01 12.8
Or using tidyverse
- slice
the rows and use floor_date
library(dplyr)
library(lubridate)
dat %>%
slice(seq(1, n(), by = 2)) %>%
mutate(V1 = floor_date(as.Date(V1), "month"))
Solution 2:
Use seq
for that:
rows_to_keep <- seq(
from = 1,
to = nrow(dat),
by = 2)
new_dat$V1 <- format(as.Date(new_dat$V1), '%Y-%m-01')
new_dat <- dat[rows_to_keep, ]
Output:
r$> new_dat
V1 V4
1 1901-01-01 12.5
3 1901-02-01 13.1
5 1901-03-01 12.8
EDIT: Added date requirement from akrun's post, which I did not understand in the original question.