0

This one should be an easy one but I cant figure out what I'm doing wrong. Probably something stupid... Sigh...

SO here it goes, i have a large tibble from which I am trying to remove some observations. I have an index logical vector that I want to use to get just the observations I want to get rid of:

 df[index,]

gives me a tibble with the observations I dont want as expected. However, I also need to access the remaining observations for which I am running

df[-index,]

This one does not work and I get back the original tibble less the first row.

What am I doing wrong? For the record, the properties of the index variable are in the next piece of code. The length of the index is as expected, i.e., the length of the original tibble.

class(index)
[1] "logical"

str(index)
logi [1:5220] TRUE FALSE .......

length(index)
[1] 5220 

dim(df)
[1] 5220 9

class(df)
[1] "tbl_df"     "tbl"        "data.frame"

Thanks

Mario Reyes
  • 385
  • 1
  • 2
  • 13

2 Answers2

2

You're dealing with a logical vector and you want the opposite of it, so you want to put NOT.

You want !index, not -index. So in your case: df[!index,]

Joy
  • 769
  • 6
  • 24
1

With tibble, we can use the filter

library(tidyverse)
df %>% 
   filter(index)

If we want to get the opposite, negate (!) it

df %>%
   filter(!index)

data

set.seed(24)
index <- sample(c(TRUE, FALSE), 10, replace = TRUE)
df <- tibble(col1 = 1:10)
akrun
  • 874,273
  • 37
  • 540
  • 662