按值选择列,然后选择3列

时间:2018-05-22 15:33:48

标签: r dplyr subset

我有一个像这样的data.table:

   Model                Variable   Coefficient standardized coefficient Model                Variable   Coefficient standardized coefficient Model
1:     0               Intercept  7.136994e+12              0.919481694   0.1               Intercept  2.201799e+12              0.918641093   0.2
2:     0 Comp_BK_Tot_Tot050500_i -1.177355e+13             -0.005086289   0.1 Comp_BK_Tot_Tot050500_i -3.632202e+12             -0.001569146   0.2
                  Variable   Coefficient standardized coefficient Model                Variable   Coefficient standardized coefficient
1:               Intercept  2.244410e+12              0.918648351   0.3               Intercept  2.258975e+12              0.918650832
2: Comp_BK_Tot_Tot050500_i -3.702495e+12             -0.001599514   0.3 Comp_BK_Tot_Tot050500_i -3.726523e+12             -0.001609894

这是我的问题,我有一个变量保存为" 0.3"为new_num。我如何编写和表达列名称"模型" contains = new_num,然后选择该列和右边的下三列?

因此,例如,我希望我的输出在这种情况下是:

Model                   Variable     Coefficient     standardized coefficient
  0.3                  Intercept    2.258975e+12                  0.918650832 
  0.3    Comp_BK_Tot_Tot050500_i   -3.726523e+12                 -0.001609894  

1 个答案:

答案 0 :(得分:1)

我们可以先使用索引(' i1')对列进行子集,然后分配'模型'使用' new_num'

的值
i1 <- match("Model", names(dt))
dt1 <- dt[, i1:(i1+3), with = FALSE][, Model := new_num][]