r - 清理数据/从数据中截断短 URL
问题描述
我目前正在清理来自 eCom 的一些 URL 数据,因为我想更好地了解引荐来源网址流量的来源。
我在 R 中尝试过 sub() 函数,但在正确应用 RegEx 时遇到了困难。
sub("*.com", "", q2$Session.First.Referrer)
我想简单地清理一个看起来像“http\://www\.gazelle\.com/main/home\.jhtml”的 URL,基本 URL 就是“www.gazelle.com”。
解决方案
我str_extract
从stringr
包中使用(tidyverse 的一部分):
library(tidyverse)
library(stringr)
my_data <- tibble(addresses = c("https://www.fivethirtyeight.com/features/is-there-still-room-in-the-democratic-primary-for-biden/",
"https://www.docs.aws.amazon.com/sagemaker/latest/dg/sms.html",
"https://www.stackoverflow.com/questions/55500553/cleaning-data-truncate-short-url-out-of-data"))
str_extract(my_data$addresses, "www.+com")
返回:
[1] "www.fivethirtyeight.com" "www.docs.aws.amazon.com"
[3] "www.stackoverflow.com"
推荐阅读
- python - python - 如何在子图中定义 xtick 频率
- javascript - Javascript对象:将索引标题分配回该索引的分配操作?
- java - maven 项目特定脚本的最佳方法?
- python - 子类可以在 pydantic 中继承其基类根验证器吗?
- zsh - 如何配置 zsh 以完成部分单词,但不区分大小写?
- python - 为什么哈希表除了数据值之外还存储键值?
- java - 如何获取 UI 的权限名称?
- java - 无法访问类“java.lang.String”。检查您的模块类路径是否存在缺失或冲突的依赖项
- python - 验证从谷歌驱动器(python)下载excel文件
- python - 在 Discord.py 机器人中记录删除消息的人