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 <- "Skinny Lasagna Rolls"
doc15 <- "Jamaican Jerk Pork Roast – Low Carb & Whole 30"
doc16 <- "Tofu & Broccoli Quinoa Stir-Fry"
doc17 <- "Chicken Fajita Stuffed Peppers"
doc18 <- "SketchBook Pinot Noir Wine"
doc19 <- "Chicken and Vegetable Soup"
doc20 <- "Manhattan Crab Chowder"
doc21 <- "Waterbrook Reserve Chardonnay"
doc22 <- "Chinese Beef and Broccoli"
doc23 <- "Easy Crab Curry"
doc24 <- "Waterbrook Reserve Chardonnay"

I want to put all of them into a R list:

as.list(c(doc1:doc24))

but I get an error:

NAs introduced by coercionNAs introduced by coercionError in doc1:doc24 : NA/NaN argument

What am I missing here?

2 Answers

1
akrun On Best Solutions

We can do this with mget using the pattern that matches a string that starts (^) with 'doc' followed by one or more digits (\\d+) till the end ($) of the string

mget(ls(pattern = "^doc\\d+$"))
1
Ronak Shah On

To get the objects in the environment you could use ls with a specific pattern

mget(ls(pattern = "^doc"))

Or you can construct them using paste0 and then use mget

mget(paste0("doc", 1:24))

#$doc1
#[1] "Hearty Chicken Chorizo, Kale, Bean and Farro Soup"

#$doc2
#[1] "Spinach, Ham and Egg Whites Frittata – 2 Points"

#$doc3
#[1] "Lentil Tabouli"

#$doc4
#[1] "Individual Brussels Sprout & Potato Frittatas"
#...