r - 在 R 中,按句号拆分文本列并按行映射到相同的 Id
问题描述
我有带有 ID 和文本的数据框 2 列。文本必须由句号分割并映射到相同的 ID。
前任
|ID. |Text |
|112 |india is highly populated. Delhi is capital of india
|113 |Tiger is wild animal.lt lives in forest
| 114 | sky is high
答案应该是
|ID | Text |
| 112| india is highly populated|
|112 |Delhi is capital of India |
|113 | Tiger is wild animal |
|113 | It lives in forest |
| 114| sky is high
你能告诉我如何进入R吗?提前谢谢
解决方案
我们可以用separate_rows
library(tidyr)
separate_rows(df1, 'Text', sep="\\.\\s*")
-输出
# A tibble: 4 x 2
# ID Text
# <dbl> <chr>
#1 112 india is highly populated
#2 112 Delhi is capital of india
#3 113 Tiger is wild animal
#4 113 lt livess in forest
数据
df1 <- structure(list(ID = c(112, 113), Text = c("india is highly populated. Delhi is capital of india",
"Tiger is wild animal.lt livess in forest")), class = "data.frame", row.names = c(NA,
-2L))
推荐阅读
- stripe-payments - 条带连接帐户订阅
- vba - 根据单击更改文本框的前景色,并在单击另一个文本框时将其返回到以前的前景色?
- c# - 将每个属性序列化为字节数组
- sql - 在 SQL 查询中重复元组
- spring - Spring集成中如何处理嵌套事务异常
- r - 在 R 中使用主键创建 SQLite 表
- python - Numpy Array Math 复制像素,而不是通过 Python 中的 For 循环进行迭代
- android - 仅在多模块项目中出现的 Jetpack Compose 性能问题
- azure-data-explorer - 按时间戳和类别取消堆叠大数据表 Kusto
- css - 颜色和空单元格或通过 CSS 将文本添加到空单元格