0

R でテキスト分析を開始しようとしています。次の構造のテキスト ファイルがあります。

HD  A YEAR Oxxxx
 WC 244 words
 PD 28 February 2018
 SN XYZ
 SC hydt
 LA English
 CY Copyright 2018 

 LP Rio de Janeiro, Feb 28



TD
   With recreational cannabis only months away from legalization in Canada, companies are racing to
   prepare for the new market. For many, this means partnerships, supply agreements,

Rで以下の要素(PDと​​TD)を抽出し、テーブルに保存したい。

これを試しましたが、正しく取得できません。

PD の抽出

library(stringr)
library(tidyverse)

pd <- unlist(str_extract_all(txt, "\\bPD\\b\t[0-9]+?\\s[A-Za-z]+?\\s[0-9]+\\s"))
pd <- str_replace_all(pd, "\\bPD\\b\t", "")
if (length(pd) == 0) {
  pd <- as.character(NA)
}
pd <- str_trim(pd)
pd <- as.Date(strptime(pd, format = "%d %B %Y"))

抽出TD

td <- unlist(str_extract_all(txt, "\\bTD\\b[\\t\\s]*?.+?\\bCO\\b"))
td <- str_replace_all(td, "\\bTD\\b[\\t\\s]+?", "")
td <- str_replace_all(td, "\\bCO\\b", "")
td <- str_replace_all(td, "\\s+", " ")
if (length(td) == 0) {
  td <- as.character(NA)

次のようなテーブルが必要です。

PD                        TD
28 February 2018          With recreational cannabis only months away from 
                          legalization in Canada, companies are racing to
                          prepare for the new market. For many, this means 
                          partnerships, supply agreements, Production hit a 
                          record 366.5Mt

どんな助けでも大歓迎です。ありがとうございました

4

1 に答える 1