-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解析器而不是临时正则表达式。 –