r - R中的正则表达式帮助 - 在每次出现数字(点)数字后拆分字符串
问题描述
我有以下示例数据框:
dat <- data.frame(date= c("Sep2020", "Oct2020", "Nov2020", "Dec2020"),
txt= c("1.1 What is the Constitution? 1.2 The original charter, which replaced the Articles of Confederation 1.3 hat all States would be equal. ",
"4.4 What is the Bill of Rights? 4.5 The 9th and 10th amendments are general ",
"5.1 in criminal prosecution to a speedy and public 5.2 War, three amendments were ratified (1865 5.3 13. The most recent amendment, the 27th, was",
"6.2 the case of the proposed equal rights amendment, the Congress exten 6.3 but the proposed Amendment was never ratifie 6.4 tification deadline. The 38th State, Michig"))
我想拆分数据框,以便在每个数字(点)数字之后创建一个新行。最终的数据框如下所示:
dat2 <-data.frame(date= c("Sep2020", "Sep2020", "Sep2020", "Oct2020", "Oct2020", "Nov2020", "Nov2020", "Nov2020", "Dec2020", "Dec2020", "Dec2020"),
txt= c("1.1 What is the Constitution?","1.2 The original charter, which replaced the Articles of Confederation","1.3 hat all States would be equal. ",
"4.4 What is the Bill of Rights?", "4.5 The 9th and 10th amendments are general ",
"5.1 in criminal prosecution to a speedy and public", "5.2 War, three amendments were ratified (1865", "5.3 13. The most recent amendment, the 27th, was",
"6.2 the case of the proposed equal rights amendment, the Congress exten", "6.3 but the proposed Amendment was never ratifie", "6.4 tification deadline. The 38th State, Michig"))
这是我到目前为止所拥有的:
dat<-dat %>%
mutate(parsed= str_extract_all(txt, "(\\d{1}\\.\\d{1,2})")) %>%
unnest(parsed)
我能得到数字,但不能得到它们之间的文本。例如,我是正则表达式的初学者,不知道如何说我想要 1.1 和 1.2 之间的所有内容。
谢谢!
解决方案
我们可能会使用separate_rows
library(tidyr)
library(dplyr)
dat %>%
separate_rows(txt, sep = "\\s+(?=\\d+\\.\\d+)")
推荐阅读
- reactjs - 反应如何滚动到特定位置
- pandas - pandas: df.loc[-1,col] 有时可以工作,有时会用 NAN 添加额外的行
- javascript - 材质 UI 样式不适用
- javascript - ES2020 nullish 合并运算符 (??) 可以用来检查未声明的变量吗?
- react-native - Firebase crashlytics 中的非致命错误能否在不杀死应用程序 React Native 的情况下记录错误
- maximo - Maximo 中的出站集成失败
- python-3.x - Sqlalchemy 反射 get_table_names() 缺少表名
- angular - 如何在 Angular 中为单位使用字符串插值
- angular - 文件加载器不适用于角度的动态 url
- react-native - 如何正确对齐同一行中的两个项目