如何在R中的data.table对象的单列中分隔两个值?
要data.table在 R中对象的单列中分隔两个值,我们可以按照以下步骤操作 -
首先,创建一个data.table对象。
然后,使用 tidyr 包中的单独函数来分隔单列中的值。
示例
创建data.table对象
让我们创建一个data.table对象,如下所示 -
library(data.table)输出结果DT<-
data.table(x=sample(c("A,B","B,C","C,D","D,E","E,F","F,G","G,H"),25,replace=TRUE))
DT
执行时,上述脚本生成以下内容output(this output will vary on your system due to randomization)-
x1: C,D
2: C,D
3: C,D
4: A,B
5: F,G
6: A,B
7: C,D
8: G,H
9: B,C
10: C,D
11: G,H
12: G,H
13: G,H
14: C,D
15: F,G
16: G,H
17: C,D
18: C,D
19: F,G
20: G,H
21: E,F
22: A,B
23: G,H
24: D,E
25: F,G
x
在列中分隔值
使用 tidyr 包中的单独函数来分隔data.table对象 DT 的x 列中的值-
library(data.table)输出结果DT<-
data.table(x=sample(c("A,B","B,C","C,D","D,E","E,F","F,G","G,H"),25,replace=TRUE))
library(tidyr)
DT %>% separate(x,c("Group1","Group2"),sep=",")
Group1 Group21: C D
2: C D
3: C D
4: A B
5: F G
6: A B
7: C D
8: G H
9: B C
10: C D
11: G H
12: G H
13: G H
14: C D
15: F G
16: G H
17: C D
18: C D
19: F G
20: G H
21: E F
22: A B
23: G H
24: D E
25: F G
Group1 Group2
以上是 如何在R中的data.table对象的单列中分隔两个值? 的全部内容, 来源链接: utcz.com/z/327396.html