Home > other >  How to remove rows with paired numbers?
How to remove rows with paired numbers?

Time:01-17

For this file:

  dat=structure(list(V1 = c("1901-01-16", "1901-01-16", "1901-02-15", 
  "1901-02-15", "1901-03-16", "1901-03-16"), V4 = c(12.5, 10, 13.1, 
  30, 12.8, 5)), row.names = c(NA, 6L), class = "data.frame")

I would like to remove the rows 2,4,6,8, until the end and replace whatever after 1901-01- by 01 output:

 1901-01-01 12.5
 1901-02-01 13.1
 1901-03-01 12.8
 

CodePudding user response:

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"))

CodePudding user response:

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.

  •  Tags:  
  • Related