setdiff删除列表中没有理由的元素?

时间:2019-05-07 14:19:32

标签: r list set-difference

给出:

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"))

并且:

query <- "Skinny Chicken Rolls"

我想从列表L中排除查询:

setdiff(L, query),但是由于某种原因,由于21中不存在query,它返回了我L个元素。

请告知我这里缺少什么。

由于某种原因,如果您尝试dput(doc_list),则只会获得一半的元素。这是怎么回事?

1 个答案:

答案 0 :(得分:3)

您的输入中有重复的元素。 setdiff返回唯一元素(因为它正在查看集合)。因此,“查询”并没有删除任何内容-您仅获得了应与unique(L)匹配的输出,因为“ Waterbrook Reserve Chardonnay”在您的列表中两次。