Search code examples
rlistset-difference

setdiff removes element without reason in a list?


Given:

doc1 <- "Hearty Chicken Chorizo, Kale, Bean and Farro Soup"

doc2 <- "Spinach, Ham and Egg Whites Frittata – 2 Points"

doc3 <- "Lentil Tabouli"

doc4 <- "Individual Brussels Sprout & Potato Frittatas"

doc5 <- "Ahi Tuna Stacks with Ginger-Soy Dressing"

doc6 <- "Sagebrush Annie's Ventura County Cabernet Sauvignon & Merlot Ventura County"

doc7 <- "Lentil Chili"

doc8 <- "Slow Cooker Lentil Stew with Sausage"

doc9 <- "Spicy Lentil and Swiss Chard Soup"

doc10 <- "Tofu-Spinach Lasagne"

doc11 <- "Baked Ziti with Spinach"

doc12 <- "Mushroom, Spinach and Cheddar Wraps"

doc13 <- "Jamaican Jerk Pork Roast – Low Carb & Whole 30"

doc14 <- "Tofu & Broccoli Quinoa Stir-Fry"

doc15 <- "Chicken Fajita Stuffed Peppers"

doc16 <- "SketchBook Pinot Noir Wine"

doc17 <- "Chicken and Vegetable Soup"

doc18 <- "Manhattan Crab Chowder"

doc19 <- "Waterbrook Reserve Chardonnay"

doc20 <- "Chinese Beef and Broccoli"

doc21 <- "Easy Crab Curry"

doc22 <- "Waterbrook Reserve Chardonnay"

# Search for all variables starting with doc and search by name of the objects, returning named list:

doc_list <- mget(ls(pattern = "^doc"))

And:

query <- "Skinny Chicken Rolls"

I want to exclude the query from the list L:

setdiff(L, query) but for some reason it returns me 21 elements given that query doesn't exists in L.

Please advise what I am missing here.

And for some reason if you will try dput(doc_list) you will get only half of the elements. What is going on here?


Solution

  • You have duplicated elements in your input. setdiff returns the unique elements (since it's looking at sets). So 'query' didn't remove anything - you're just getting the output that should match unique(L) since "Waterbrook Reserve Chardonnay" is in your list twice.