没有分隔符且长度不均匀的分割值

时间:2018-09-12 18:11:23

标签: r split gsub tidyr strsplit

我从以下站点抓取了第二个HTML表

http://www.floridahealth.gov/programs-and-services/office-of-medical-marijuana-use/medical-marijuana-treatment-centers/index.html

下面是我提取表的代码:

   FLlist <- read_html("http://www.floridahealth.gov/programs-and- 
services/office-of-medical-marijuana-use/medical-marijuana-treatment- 
centers/index.html")

FLDispensaries <- as.data.frame(FLlist %>%
  html_nodes("table") %>%
  .[[2]] %>%
  html_table(header = TRUE))

write.csv(FLtest, "FLTest.csv")

该表格在Excel中填充,如下所示:List after is written as a CSV

我想分隔地址信息,但长度或分隔符不一致。该列表将继续增长,并且我遇到了与其他数据集相同的问题,因此我想编写一个代码来自动执行地址数据的分离过程。也许当我最初刮擦桌子时可以做到这一点吗?

1 个答案:

答案 0 :(得分:0)

由于您尚未指定要如何分隔地址,因此我假定地址块中的每一行都应放在单独的列中。
为此,您可以选择具有足够XPath查询的第二张表的p标签,例如

//*[@id="DispensingFacilities"]/tbody/tr/td/p/text()

,然后遍历结果并从头开始创建表结构。现在,以这种方式生产CSV的工作要多一些。有几种方法,如下所示:

library(xml2)
library(magrittr)
library(rvest)

FLlist <- read_html("http://www.floridahealth.gov/programs-and-services/office-of-medical-marijuana-use/medical-marijuana-treatment-centers/index.html")

FLDispensaries <- as.data.frame(FLlist %>%
                                  html_nodes("table") %>%
                                  .[[2]] %>%
                                  html_table(header = TRUE))

FLTable <- html_nodes(x=FLlist, xpath='//*[@id="DispensingFacilities"]/tbody/tr/td/p/text()')

#helper
trim <- function (x) gsub("^\\s+|\\s+$", "", x)

mat<-matrix(list(), ncol=4)
li <- c()
row <- 0;
col <- 1;
for(i in FLTable){
  li <- c(li,trim(html_text(i)));
  if(col %% 4 == 0) {
    row <- row + 1;
    mat[[row]] <- li;
    li <- c();
  }
  col <- col + 1;
}
#to matrix/list to dataframe
library(plyr)
mat.df <- as.data.frame(do.call(rbind, mat))
write.csv(mat.df, "FLTest.csv")

由于您只想拥有实际地址,因此我将XPath更改为仅从第二个td开始选择第二个tr

FLTable <- html_nodes(x=FLlist, xpath='//*[@id="DispensingFacilities"]/tbody/tr[position()>1]/td[2]/p')

li <- c()
row <- 1;
mat<-matrix(list(), ncol=2)
for (i in seq_along(FLTable)) {
  addrlines <- str_split(xml_text(FLTable[[i]]), "\\s\\s")
  for (a in seq_along(addrlines[[1]])) {
    if(a %% 2 == 0) {
      li <- c(li,paste(addrlines[[1]][a], addrlines[[1]][a+1]));
      mat[[row]] <- li;
      li <- c();
      row <- row + 1;
    } else if(a %% 3 == 0) {
      next; #skip
    } else {
      li <- c(li,addrlines[[1]][a]);
    }
  }
}

并创建一个不错的输出

outputFile <- "output.csv"
#to matrix/list to dataframe
mat.df <- as.data.frame(do.call(rbind, mat))
cat(c("No.,", "Name,", "Address"), '\n',  file = outputFile)
write.table(mat.df,outputFile,sep=',',append = T, col.names = F)

生成的CSV如下所示:

No., Name, Address 
"1","AltMed Florida (MuV)","5909 U.S. Hwy 41 N Apollo Beach, FL 33572"
"2","Trulieve","1103 14th Street West Bradenton, FL 34205"
...
"55","Trulieve","1814 Commerce Avenue Vero Beach, FL 32960"