【发布时间】:2014-08-28 11:46:28
【问题描述】:
我有一个像这样的大型 data.frame (df):
A B C D E
1.2 2.2 3.3 4.4 5.2
4.2 5.3 6.5 7.2 1.5
2 7 3 4 6
1 2 3 4 5
我想通过在 R 中使用 dplyr 和以下代码来创建 B 列的三分位组:
第一
library('dplyr')
ntile(df$B, 3)
所以我从 B 列得到了三个组:
#group 1
ntile(df$B,3)==1
#group2
ntile(df$B,3)==2
#group3
ntile(df$B,3)==3
现在我想通过加入组 1,2 成为一个组和组 3 成为第二组来生成二进制变量。 我应用了这段代码:
第二
#combine group1,2
bin1 <- c((ntile(df$B,3)==1),(ntile(df$B,3)==2))
#creating the second group
bin2 <- (ntile(df$B,3)==3)
我只是想确定我是否做对了(在第一部分和第二部分)?我想知道是否有任何其他(更快/更容易)的方法来做到这一点?通过使用(dplyr 或 R 中的任何其他包)
【问题讨论】: