1

So I'm trying to create a function which will take an argument from a list and return it into a dataframe. So far I got close with this sample code, but i've become stuck for a few weeks now.
# list of pets
pets = list("cats", "dogs")

# comment link
search_url = paste("https://duckduckgo.com/?q=", pets[])

# empty data frame for the pets
petsX <- data.frame()

# function for x in pets
getSearch <- function(x) {
  petsX <- ( search_url [x] )
}

getSearch(1)
# I want this to be:
# https://duckduckgo.com/?q=cats

Any help or guidance in the right direction would be truly appreciated.

2
  • Hi M4rk3h11! Welcome to stack overflow. It's a bit unclear where/if an error occurs and what the desired output should be (apart from your code comment). Maybe put an error and a "desired output" in separate code boxes? Also consider creating a question title that's more problem-specific to get attention from the right people. Commented Feb 22, 2021 at 16:33
  • Hi Andre, thanks for the feedback. I'll try to be more specific from now on :) Commented Feb 23, 2021 at 10:05

1 Answer 1

1

I am not sure why you need the petsX dataframe at all. You already have all search terms in the list. It would probably be better if you store the results of the function in dataframe when you use it.

# list of pets
pets = list("cats", "dogs")

# comment link
search_url = paste("https://duckduckgo.com/?q=", pets, sep = "")

# function for x in pets
getSearch <- function(x) {
  search_url[[x]]
}

getSearch(1)
#> [1] "https://duckduckgo.com/?q=cats"
getSearch(2)
#> [1] "https://duckduckgo.com/?q=dogs"

Created on 2021-02-22 by the reprex package (v0.3.0)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.