我废弃了一个Web,现在我需要清理"服务"列,它是一个字符串.

在fl_data数据集中的service列中,您可以看到有多个服务,如Testing Services和Testing Services.这些服务介于\n和:之间,但并非所有行都有所有服务.

我需要将字符串划分为列,每个列都应该有一种服务类型及其元素.

这是我的数据集:

url_base <- "https://npin.cdc.gov/search?=type%3Aorganization&page="

map_df(0:0, function(i) {

  cat(".")

  pg <- read_html(sprintf(url_base, i))

  data.frame(org_name = html_text2(html_nodes(pg, ".block-field-blocknodeorganizationtitle")),
             street = html_text(html_nodes(pg, ".address-line1")),
             city = html_text(html_nodes(pg, ".locality")),
             state = html_text(html_nodes(pg, ".administrative-area")),
             zip = html_text(html_nodes(pg, ".postal-code")),
             service = html_text2(html_nodes(pg, ".services-fieldset")),
             stringsAsFactors=FALSE
             )

}) -> raw_data

fl_data <- raw_data |> 
  filter(state=="FL") |> 
  mutate(service = str_remove(service, "Services\nPlease contact organization for eligibility requirements"))

推荐答案

您可以使用for循环来提取服务和相应的项.在result中,项目之间用,分隔.

library(tidyverse)
library(rvest)

url <- "https://npin.cdc.gov/search?=type%3Aorganization&page=0"
content <- read_html(url)

services <- content %>% html_nodes(".services-fieldset")
org_name <- content %>% html_nodes(".block-field-blocknodeorganizationtitle") %>% html_text2()


result <- data.frame(org_name = as.integer(), service = as.character(), item = as.character())

for (i in 1:length(services)) {
  temp <- services[i] %>% html_nodes(".field__items")
  for (j in 2:length(temp)) {
    label <- temp[j] %>% html_nodes(".field-label") %>% html_text() %>% gsub(":", "", .)
    items <- temp[j] %>% html_nodes(".field__item") %>% html_text()
    result[nrow(result) +1, ] <- c(org_name[i], label, paste0(items, collapse = ","))
  }
}

result |> tibble()
#> # A tibble: 32 × 3
#>    org_name                          service                     item           
#>    <chr>                             <chr>                       <chr>          
#>  1 Eastport Health Care Incorporated Testing Services            Gonorrhea Test…
#>  2 Eastport Health Care Incorporated Care and Treatment Services Family Plannin…
#>  3 Alamosa County Public Health      Testing Services            TB Testing     
#>  4 Alamosa County Public Health      Care and Treatment Services Mpox Vaccine,H…
#>  5 Alamo Navajo Health Center        Testing Services            TB Testing,Gon…
#>  6 Alamo Navajo Health Center        Prevention Services         TB Prevention/…
#>  7 Alamo Navajo Health Center        Care and Treatment Services Family Plannin…
#>  8 AIDS Resource Group               Testing Services            Hepatitis C Te…
#>  9 AIDS Resource Group               Prevention Services         STD/STI Preven…
#> 10 AIDS Resource Group               Support Services            Support Groups…
#> # ℹ 22 more rows

创建于2024—03—14,reprex v2.1.0

R相关问答推荐

单击 map 后,将坐标复制到剪贴板

矩阵%*%矩阵中的错误:需要数字/复杂矩阵/向量参数

在发布到PowerBI Service时,是否可以使用R脚本作为PowerBI的数据源?

如何在R中添加截止点到ROC曲线图?

然后根据不同的列值有条件地执行函数

如何在ggplot中标记qqplot上的点?

如何在观测缺失的地方添加零

将文件保存到新文件夹时,切换r设置以不必创建目录

如何同时从多个列表中获取名字?

如何通过匹配R中所有可能的组合来从宽到长旋转多个列?

在R函数中使用加号

以NA为通配符的R中的FULL_JOIN以匹配其他数据中的任何值.Frame

如何从向量构造一系列双边公式

如何将一列中的值拆分到R中各自的列中

有没有办法一次粘贴所有列

我是否可以使用多个变异项来构建顺序列(标记多个问题)

我需要使用ggplot2制作堆叠条形图

如果极点中存在部分匹配,则替换整个字符串

reshape 数据帧-基于组将行转换为列

如何修改Rust中的R字符串并将其赋给新的R变量,并使用extendr保留原始R字符串