Reputation: 4243
I have a data.table like this:
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
Here is my issue, I have a variable saved as "0.3" as new_num
. How do I write and expression where if the column name "Model" contains = new_num, then select that column and the next 3 columns to the right of it?
So for example, I want my output in this case to be:
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
Upvotes: 2
Views: 40
Reputation: 887118
We can subset the columns first with an index ('i1') and then assign 'Model' with the values of 'new_num'
i1 <- match("Model", names(dt))
dt1 <- dt[, i1:(i1+3), with = FALSE][, Model := new_num][]
Upvotes: 1