首页 > 解决方案 > 基于 $ 提取单词并取消嵌套数据

问题描述

我正在尝试对我的数据做两件事。数据如下:

# A tibble: 10 x 2
     grp newCol                                                                     
   <int> <chr>                                                                      
 1  6303 "The company sees earnings of $3.40 to $3.60 a share for all of 2008, agai…
 2  7686 "   -- reaffirmed its fiscal 2015 guidance of per share diluted earnings b…
 3  9577 "Analysts polled by FactSet anticipate earnings of 96 cents a share, down …
 4  6475 ""                                                                         
 5  5229 "The company also expects income to be \"significantly impacted\" by costs…
 6  2648 "Hoku also expects losses for the foreseeable future on significant cost i…
 7  3691 "St. Louis-based Emerson now sees full-year earnings of $2.40 to $2.60 a s…
 8  9297 ""                                                                         
 9  2080 "The restaurant group also raised its earnings guidance for fiscal 2007 to…
10  3513 "Guidance, For the full fiscal year 2008, the Company is moderating its pr…

我可以运行以下命令:

x <- d %>% 
  mutate(
    extractedWords = str_extract_all(newCol, "([^\\s]+\\s){2}earnings(\\s[^\\s]+){12}")
  )

我在哪里得到:

# A tibble: 10 x 1
   extractedWords
   <list>        
 1 <chr [1]>     
 2 <chr [3]>     
 3 <chr [1]>     
 4 <chr [0]>     
 5 <chr [0]>     
 6 <chr [0]>     
 7 <chr [1]>     
 8 <chr [0]>     
 9 <chr [2]>     
10 <chr [4]>  

我首先要修改str_extract_all(newCol, "([^\\s]+\\s){2}earnings(\\s[^\\s]+){12}")- 当前提取 Word 之前的 2 个单词和 Wordearnings之后的 12 个单词earnings。我想对其进行更改,使其提取美元$符号之前的单词。

其次,我想要unnest列。当我运行时:

x %>% 
  unnest(extractedWords)

数据的行数从1012。我想要unnest它,但将其粘贴c("text", more text")到类似text, more text或分隔|(或某些变体)的东西中。

数据:

d <- structure(list(grp = c(6303L, 7686L, 9577L, 6475L, 5229L, 2648L, 
3691L, 9297L, 2080L, 3513L), newCol = c("The company sees earnings of $3.40 to $3.60 a share for all of 2008, against $2.74 a share from continuing operations in 2007, an increase of 24% to 31%.   ", 
"   -- reaffirmed its fiscal 2015 guidance of per share diluted earnings between , We believe the guidance outlook for fiscal 2015 remains realistic and takes into consideration the heightened competitive market trends for the Diagnostics segment offset by strategic investments that target the growing outpatient segment and further growth of our Life Science segment through our focus on global expansion, increasing industrial market efforts, and emerging success in the AgriBio and genomics research areas., FISCAL 2015 GUIDANCE REAFFIRMED, For the fiscal year ending September 30, 2015, management expects net revenues to be in the range of $193 million to $200 million and per share diluted earnings to be between $0.85 and $0.91. The per share estimates assume an increase in average diluted shares outstanding from approximately 41.9 million at fiscal 2014 year end to approximately 42.4 million at fiscal 2015 year end. The revenue and earnings guidance provided in this press release is from expected internal growth and does not include the impact of any additional acquisitions the Company might complete during fiscal 2015.", 
"Analysts polled by FactSet anticipate earnings of 96 cents a share, down 10 cents from a year earlier. Revenue is expected to have decreased 5.5% to $30.4 billion, which would mark the fourth consecutive quarterly decline after six years of growth. Verizon has said it expects earnings and sales will be roughly flat this year.", 
"", "The company also expects income to be \"significantly impacted\" by costs related to its pending acquisition of SRS Labs Inc. (SRSL)., The company also expects income to be \"significantly impacted\" by costs related to its pending acquisition of SRS Labs Inc. (SRSL).", 
"Hoku also expects losses for the foreseeable future on significant cost increases.   ", 
"St. Louis-based Emerson now sees full-year earnings of $2.40 to $2.60 a share, down from its February forecast of $2.70 to $2.95 a share. The company also expects net sales for the fiscal year to fall 13% to 15% to $21 billion to $21.7 billion. Sales are expected to be hurt by about 5% because of currency translations, but boosted by 1% because of acquisitions.", 
"", "The restaurant group also raised its earnings guidance for fiscal 2007 to a range of $3.45 a share to $3.50 a share, and said its expects earnings for the third quarter ending in July of 85 cents to 89 cents a share and same-store sales growth of 5% to 6%.   ", 
"Guidance, For the full fiscal year 2008, the Company is moderating its previously issued guidance and expects net sales to be approximately $950 million and earnings per diluted share to be approximately $1.00, which includes approximately $0.45 per diluted share of restructuring charges and other unusual items. For the twelve months ended February 2, 2008, net sales were $1.09 billion and earnings per diluted share were $2.59., Set forth below is our reconciliation of net earnings per share, calculated in accordance with generally accepted accounting principles, or GAAP, to net earnings per share, as adjusted, for certain historical periods and certain future periods. For reference, we also include our previous guidance for third quarter fiscal 2008. Net earnings per share, as adjusted, excludes (i) the net impact of certain restructuring costs and other unusual items as well as the write off of unamortized financing costs during the first three quarters of fiscal 2008 and (ii) the anticipated impact of certain restructuring costs in the fourth quarter of fiscal 2008. We believe that investors often look at ongoing operations as a measure of assessing performance and as a basis for comparing past results against future results. Therefore, we believe that presenting our results and expected results excluding these items provides useful information to investors because this allows investors to make decisions based on our ongoing operations. We use the results excluding these items to discuss our business with investment institutions, our board of directors and others. Further, we believe that presenting our results and expected results excluding these items provides useful information to investors because this allows investors to compare our results and our expected results for the periods presented to other periods.,                           Guidance   Results      for      Guidance   Guidance"
)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-10L))

标签: r

解决方案


我不是 100% 确定你对问题的第一部分的意思。假设您要提取 word 之后earnings和之前的所有单词$,这应该可以满足您的要求。它使用“正向前瞻”并允许任意数量的单词,直到找到第一个美元符号(因此是*?)。

我不是unnesting,而是使用 循环遍历extractedWordspurrr::map_chr,它返回一个字符向量,这使得进一步的取消嵌套变得不必要。

library(tidyverse)

d %>% 
  mutate(
    extractedWords = str_extract_all(newCol, "([^\\s]+\\s){2}\\$(\\s?[^\\s]+){12}")
  ) %>% 
  mutate(result = map_chr(extractedWords, str_c, "", collapse="|"))

编辑:编辑正则表达式以提取美元符号之前的 2 个单词和之后的 12 个单词。请注意,我必须转义美元符号 ( \\$) 才能正常工作,因为美元符号在正则表达式中具有特殊含义。


推荐阅读