【发布时间】:2020-09-21 14:35:58
【问题描述】:
我有一些文本数据,如下所示:
text
1 to $2.00 on an ongoing basis. the ongoing eps guidance excludes both a 68 cent-per-share charge associated with the establishment of the solutia-related reserve and a tax benefit of
2 wheat and barley business. on a reported basis, eps is in the range of $1.56 to $1.71 per share for the full fiscal year. (for a reconciliation of ongoing... eps was 4.56 to 4.98
3 the year ago quarter while 2004 full year eps was $.93, up 7.7% from 2003. return on equity was 21.7% for the fourth quarter and 20.4% for the full
我正在尝试从中提取一些信息。我想提取单词eps 之后的第一个数字。我可以做到以下几点:
data %>%
mutate(
firstNumberAfterWord = str_match_all(text, "eps\\D*(\\d+)")
)
这给出了:
firstNumberAfterWord
1 eps guidance excludes both a 68, 68
2 eps is in the range of $1, 1 # This is wrong. It should be "$1.56 to $1.71"
3 eps was $.93, 93
这不符合我的要求,因为它拉动了 68、1 和 93,但 1 不正确。我查看了map_chr(myWordColumn, str_c, collapse = "\n"),,首先将其折叠,然后提取单词,但没有运气。
我想提取eps 单词之后的第一个数字(eps 单词的所有出现,其中每个出现由"\n" 分隔符分隔。
预期的输出将是有一个新列,其中包含:
$.93 # since this comes after the part "eps was $.93"
68 # since it comes after "eps guidance excludes both a 68"
$1.56 to $ 1.71 # "eps is in the range of $1.56 to $1.71" # On a new line for this observation
4.56 to 4.98 # eps was 4.56 to 4.98
这些都在eps 之后。
数据:
data <- data.frame(
text = c(" to $2.00 on an ongoing basis. the ongoing eps guidance excludes both a 68 cent-per-share charge associated with the establishment of the solutia-related reserve and a tax benefit of",
" wheat and barley business. on a reported basis, eps is in the range of $1.56 to $1.71 per share for the full fiscal year. (for a reconciliation of ongoing... eps was 4.56 to 4.98",
" the year ago quarter while 2004 full year eps was $.93, up 7.7% from 2003. return on equity was 21.7% for the fourth quarter and 20.4% for the full"
)
)
【问题讨论】: