如何在R中的data.table对象列中分隔特定文本和剩余文本?
要将特定文本和data.tableR中对象列中的剩余文本分开,我们可以按照以下步骤操作 -
首先,创建一个data.table对象。
然后,使用 stringr 包中的 str_split 函数将第一个文本值和剩余文本分开。
示例
创建data.table对象
让我们创建一个data.table对象,如下所示 -
library(data.table)输出结果Student_ID<-
sample(c("1Rosy","3Numan","8John","2Shera","9Hina"),25,replace=TRUE)
DT<-data.table(Student_ID)
DT
执行时,上述脚本生成以下内容output(this output will vary on your system due to randomization)-
Student_ID1: 9Hina
2: 1Rosy
3: 3Numan
4: 9Hina
5: 8John
6: 9Hina
7: 9Hina
8: 1Rosy
9: 3Numan
10: 8John
11: 9Hina
12: 1Rosy
13: 9Hina
14: 8John
15: 1Rosy
16: 2Shera
17: 1Rosy
18: 3Numan
19: 8John
20: 1Rosy
21: 9Hina
22: 8John
23: 8John
24: 8John
25: 9Hina
Student_ID
将字符串列中的第一个值和其余文本分开
使用 stringr 包中的 str_split 函数将data.table对象 DT 中Student_ID 列的每一行中的第一个文本值和剩余文本分开-
library(data.table)输出结果Student_ID<-
sample(c("1Rosy","3Numan","8John","2Shera","9Hina"),25,replace=TRUE)
DT<-data.table(Student_ID)
library(stringr)
DT$ID_Name<-str_split(DT$Student_ID,"(?<=.{1})",2)
DT
Student_ID ID_Name1: 9Hina 9, Hina
2: 1Rosy 1, Rosy
3: 3Numan 3, Numan
4: 9Hina 9, Hina
5: 8John 8, John
6: 9Hina 9, Hina
7: 9Hina 9, Hina
8: 1Rosy 1, Rosy
9: 3Numan 3, Numan
10: 8John 8, John
11: 9Hina 9, Hina
12: 1Rosy 1, Rosy
13: 9Hina 9, Hina
14: 8John 8, John
15: 1Rosy 1, Rosy
16: 2Shera 2, Shera
17: 1Rosy 1, Rosy
18: 3Numan 3 ,Numan
19: 8John 8, John
20: 1Rosy 1, Rosy
21: 9Hina 9, Hina
22: 8John 8, John
23: 8John 8, John
24: 8John 8, John
25: 9Hina 9, Hina
Student_ID ID_Name
以上是 如何在R中的data.table对象列中分隔特定文本和剩余文本? 的全部内容, 来源链接: utcz.com/z/347490.html