[英]Using data.table and cut to split a variable into groups with equal observations
I have a question which is very simple. 我有一个非常简单的问题。 With the answers to more complicated related questions I have not been able to figure out the right syntax.
对于更复杂的相关问题的答案,我一直无法弄清楚正确的语法。
I have a data.table which looks as follows: 我有一个data.table,如下所示:
dat <- read.table(
text = "A B C D E F G H I J
A 0 1 1 1 0 1 0 1 1 1
B 1 0 0 0 1 0 1 0 0 2
C 0 0 0 1 1 0 0 0 0 3
D 1 0 1 0 0 1 0 1 0 4
E 0 1 0 1 0 1 1 0 1 5
F 0 0 1 0 0 0 1 0 0 6
G 0 1 0 1 0 0 0 0 0 7
H 1 0 1 0 0 1 0 0 0 8
I 0 1 0 1 1 0 1 0 0 9
J 1 0 1 0 0 1 0 1 0 9",
header = TRUE
)
Now I would like to use data.table to create a variable called Jcat
to divide variable J
into 3 categories with more or less equal amount of observations, simply: 现在我想用data.table创建一个名为变量
Jcat
到可变划分J
分为3类具有更多或更少等量的观察,只需:
dat <- read.table(
text = "A B C D E F G H I J Jcat
A 0 1 1 1 0 1 0 1 1 1 1
B 1 0 0 0 1 0 1 0 0 2 1
C 0 0 0 1 1 0 0 0 0 3 1
D 1 0 1 0 0 1 0 1 0 4 2
E 0 1 0 1 0 1 1 0 1 5 2
F 0 0 1 0 0 0 1 0 0 6 2
G 0 1 0 1 0 0 0 0 0 7 3
H 1 0 1 0 0 1 0 0 0 8 3
I 0 1 0 1 1 0 1 0 0 9 3
J 1 0 1 0 0 1 0 1 0 9 3",
header = TRUE
)
I am struggling with the syntax. 我在语法上苦苦挣扎。
What would be the simplest way to do this? 最简单的方法是什么?
We can specify the number of breaks in breaks
argument of cut
我们可以指定
cut
的breaks中的breaks
个数
library(data.table)
n <- 3
setDT(dat)[, Jcat := as.integer(cut(J, breaks = n))]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.