在 R 中,通过句号拆分文本列并映射到与我相同的行 [英] In R, Splitting text column by fullstop and mapping to the same I'd by rows

查看:35
本文介绍了在 R 中,通过句号拆分文本列并映射到与我相同的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有带有 ID 和文本的数据框 2 列.文本必须用句号分割并映射到相同的 ID.

I have dataframe 2 columns with ID and Text. Text has to be split by fullstop and mapped with to same 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.提前致谢

Can you please let me know how to get in R. Thanks in advance

推荐答案

我们可以使用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))

这篇关于在 R 中,通过句号拆分文本列并映射到与我相同的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆