如何在R的data.table对象列中分离特定文本和其余文本?
为了在R的data.table对象列中分离特定文本和其余文本,我们可以按照以下步骤操作:
首先,创建一个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
输出
执行上述脚本后,将生成以下输出(由于随机化,此输出将因您的系统而异):
Student_ID 1: 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_Name 1: 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
广告