Reputation: 23
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.
Upvotes: 1
Views: 41
Reputation: 2609
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)
Upvotes: 1