[英]Add to text in url based on a vector of character strings in r
Suppose you have a vector of character strings such as a series of locations like below... 假设您有一个字符串向量,例如下面的一系列位置...
cities<-c("San Diego County","Fresno County", "Los Angeles County",
"Lake County")
...and additionally you have some url such as www.fakewebsite.com/search_loc=
. ...此外,您还有一些网址,例如
www.fakewebsite.com/search_loc=
。 What would the appropriate syntax be to paste each value from the "cities" vector to the end of the url (following search_loc=
) such that all spaces between strings are separated with the following $50
and for each url to end with the following %100
. 合适的语法是将“城市”矢量中的每个值粘贴到url的末尾(跟随
search_loc=
),以使字符串之间的所有空格都由以下$50
分隔,并且每个url以以下%100
结尾。 Ideally, I would like the result to look like the following: 理想情况下,我希望结果如下所示:
cities url
San Diego County www.fakewebsite.com/search_loc=San$50Diego$50County%100
Fresno County www.fakewebsite.com/search_loc=Fresno$50County%100
Los Angeles County www.fakewebsite.com/search_loc=Los$50Angeles$50County%100
Lake County www.fakewebsite.com/search_loc=Lake$50County%100
Note: The cities
vector has a maximum of three separate strings (ie two white spaces) and a minimum of two separate strings (ie one white space). 注意:
cities
矢量最多包含三个单独的字符串(即两个空格),最少包含两个单独的字符串(即一个空格)。
Using paste0
and gsub
, we can replace empty spaces with "$50" and end it with "%100" 使用
paste0
和gsub
,我们可以用“ $ 50”替换空白,并用“%100”结束
paste0(base_url, gsub("\\s+", "$50", cities), "%100")
#[1] "www.fakewebsite.com/search_loc=San$50Diego$50County%100"
#[2] "www.fakewebsite.com/search_loc=Fresno$50County%100"
#[3] "www.fakewebsite.com/search_loc=Los$50Angeles$50County%100"
#[4] "www.fakewebsite.com/search_loc=Lake$50County%100"
data 数据
cities<-c("San Diego County","Fresno County", "Los Angeles County",
"Lake County")
base_url <- "www.fakewebsite.com/search_loc="
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.