-1
我想轉換對應唯一的用戶ID一些鏈接:如何替換/重命名給定字符串模式的行?
df<- data.frame(
employeeId = c(1,2,3,4,5,6),
linkToEmployee = c("http://intranet.homepageEmploye.com/herSalary",
"http://intranet.homepageEmploye.org/herSalary/Details",
"http://local.com/qa/for",
"here the homepage is missing",
"http://local.org/",
"here the homepage is missing"))
employeeId linkToEmployee
1 1 http://intranet.homepageEmploye.com/herSalary
2 2 http://intranet.homepageEmploye.org/herSalary/Details
3 3 http://local.com/qa/for
4 4 here the homepage is missing
5 5 http://local.org/
6 6 here the homepage is missing
現在我想這些鏈接轉換爲表格:
desired<- data.frame(
employeeId = c(1,2,3,4,5,6),
linkToEmployee = c("intranet.com",
"intranet.org",
"local.com",
"here",
"local.org",
"here"))
employeeId linkToEmployee
1 1 intranet.com
2 2 intranet.org
3 3 local.com
4 4 here
5 5 local.org
6 6 here
我曾嘗試使用gsub
內聯網的情況下,但這似乎並沒有按預期工作。
df$linkToEmployee <- gsub("http://intranet.homepageEmploye.com/", "intranet.com.", df$linkToEmployee)
然而,這並不按預期方式工作要做到這一點
特殊字符:。和/會搞亂你的gsub。它應該工作,如果你添加選項'fixed = TRUE'來停止它將其解釋爲正則表達式字符串。 –
什麼*確切*是您想要的結果?你的例子並沒有真正說明這一點,因爲它沒有概括。無論如何,您應該使用適當的URI解析器而不是臨時正則表達式。 –