0

Web スクレイピングされたさまざまなテーブルを行バインドして、データ フレームを作成しました。

# html files
filelist <- c("Prod223_2688_00185641_20190930.html","Prod224_0078_SO305092_20191130.html", 
"Prod224_0078_SO305426_20190831.html", "Prod224_0078_SO305431_20190831.html", 
"Prod224_0078_SO305440_20190831.html", "Prod224_0078_SO305451_20200331.html", 
"Prod224_0078_SO306088_20190531.html", "Prod224_0078_SO306098_20180630.html", 
"Prod224_0078_SO306098_20190630.html", "Prod224_0078_SO306411_20190530.html")

# web scraping tables
mydata <- lapply(filelist, function(x) {
  read_html(x) %>% rvest::html_table(fill = T) %>% 
    dplyr::nth(2) 
})

# row binding (adding a new column with row .id)
mydata <- rbindlist(mydata, idcol=T, fill = T) 

行 .id に基づいてcompany、それぞれの名前で新しい列を作成したいと考えています。filelistと の間の 3 番目のコードが会社名です_。このようなものを取得するには:

mydata
 company  id.  X2 ..
00185641    1  .. 
00185641    1  .. 
SO305092    2  .. 
SO305426    3  .. 
SO305426    3  .. 

これは非常に単純な質問かもしれませんが、R の関数についてはまだ自信がありません。私はこの同様の質問を見て、試しました:

mydata2 <- mydata2 %>% mutate(company=lapply(mydata2,filelist))
# and this:
mydata2$company <- rep(paste(filelist), length(mydata2$.id))
4

1 に答える 1