filter() or select()

Hi , I need to select from a dataset the observations not in a list , how can use filter() or select() do it

We need more information and a reproducible example (reprex)

library(tidyverse)
dim(iris)
#> [1] 150   5
unique(iris$Species)
#> [1] setosa     versicolor virginica 
#> Levels: setosa versicolor virginica
dim(iris %>% filter(!(Species %in% c("setosa", "versicolor"))))
#> [1] 50  5

Created on 2021-07-01 by the reprex package (v1.0.0)

library(dplyr)
#library(stringr)
#library(gapminder)
library(tidyverse)
library(datasets)

#Lista_de_especies_RNP_Clements <- read_excel("CIAMB/TESIS/Data/Lista de especies RNP-Clements.xlsx")
#attach(paa_codazzi3)

paa_codazzi2 <- paa_codazzi %>%
rename(Entidad = ...1,Codigo = ...2, Descripcion=...3,Fecinicpro = ...4,fecpofer = ...5,plazo = ...6,unidad_plazo = ...7,modalidad = ...8,fuente = ...9,valor =...10,vlrvigactu = ...11)

list_codigos<-c("42203605","4323","81111","81112","81122","81123","81161501")
list_codexclude<-c("81111508","81111811","81111808")

codigo8 <- c(42203605,81161501)
#codigo5 <- c(81111,81112,81122,81123)
#codigo4 <- c(4323)

paa_codazzi3 <- paa_codazzi2 %>%
mutate(indicator = "X",
Codigo = as.character(Codigo), codigo4=substr(Codigo,1,4),codigo5=substr(Codigo,1,5)) %>%
filter(Codigo %in% list_codigos | codigo4 %in% list_codigos | codigo5 %in% list_codigos)

paa_codazzifinal <- paa_codazzi3 %>%
filter(Codigo != list_codexclude )

The last sentence is not working

The code isn't reproducible because we don't have your files, but perhaps:

# edited code
paa_codazzifinal <- paa_codazzi3 %>%
  filter(!Codigo %in% list_codexclude)


# example
library(dplyr)
aa <- c("setosa", "versicolor") # to be excluded
iris %>% 
  filter(!Species %in% aa) # species not in the list - shows virginica

Thank you so much Arthur

Thank you so much William

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.