I have 891 tif images which I list them. I want to remove 10 specific images from the list based on their names (i.e., in the end I want my list to have 881 images). I tried many things but I am getting errors based on what I've tried. For example, my last try was:
as this is vectorised on the l, and will exclude any member of l that is not in the remove list. However , I would expect your remove list to contain items ending .tif and yours dont ? "pop.tif" for example
If "pop" is not meant to be the complete name but just a detectable part then you will need to use some form of regex, probably most conveniently through the stringr library to do the matching as = \ != and %in% are all going to check that two items are the same or different, and not look to see if they have common or differing parts
@nirgrahamuk I changed the elements in remove by adding .tif and then I used your code l[ ! l %in% remove ]. In the Global environment, the l still has 891 elements but in the console the list prints 881. Is this correct?
I am asking this because if I remove the elements manually, one by one, by typing for example l <- l[-1] (for the agbh.tif element to be removed), then the l on the Global envirnment has 890 elements.
# full.names doesn't mean the name with the file
# extension stripped; it means the file name without the
# full path name; I'm going to use the full
# path so that reprex will be able to find it
# call it v for vector, not l for list
(v = list.files("/Users/ro/projects/demo",pattern = ".csv$", full.names = FALSE))
#> [1] "bio.csv" "block.data.csv" "datitot.csv" "dframe.csv"
#> [5] "foo.csv" "grist.csv" "msptemps.csv" "mtcars.csv"
#> [9] "s.csv" "t3.csv" "ukgas.csv"
# specify the file names to remove including the
# extension
(remove <- c("foo.csv","grist.csv","mtcars.csv"))
#> [1] "foo.csv" "grist.csv" "mtcars.csv"
# use set difference to find members of v that
# are not in remove
setdiff(v,remove)
#> [1] "bio.csv" "block.data.csv" "datitot.csv" "dframe.csv"
#> [5] "msptemps.csv" "s.csv" "t3.csv" "ukgas.csv"