Home > Net >  Dummy elements for missing elements to keep a function running
Dummy elements for missing elements to keep a function running

Time:02-05

I recreated an error that I've been getting. I am well aware that the error is indicating that element 6 in int1 is missing, and so it is unable to remove it. My actual script leads to this error when comparing two data sets that are not the same length (unsure how to recreate it). I was wondering if there is some way to plug in a dummy element into the spots that are missing an element.

Error that I am getting:

Error in `stop_subscript()`:
! Can't negate elements that don't exist.
x Location 6 doesn't exist.
i There are only 5 elements.
Run `rlang::last_error()` to see where the error occurred.
library(lubridate)
library(tidyverse)
library(purrr)

date <- rep_len(seq(dmy("01-01-2011"), dmy("31-07-2011"), by = "days"), 100)
ID <- rep(c("A","B"), 100)
df <- data.frame(date = date,
                 x = runif(length(date), min = 60000, max = 80000),
                 y = runif(length(date), min = 800000, max = 900000),
                 ID)

df$Month <- month(df$date)

int1 <- df %>%
  # arrange(ID) %>%   # skipped for readability of result
  mutate(new = floor_date(date, '10 day')) %>%
  mutate(new = if_else(day(new) == 31, new - days(10), new)) %>% 
  group_by(ID, new) %>%
  filter(Month == "1") %>% 
  group_split()

int1 <- int1[-2]

int1 <- int1[-6]

CodePudding user response:

Suppose you want to remove the elements in vector x. Then you can try :

int1[setdiff(seq_along(int1), x)]
  •  Tags:  
  • Related