Home > Software design >  How to convert a row of binary indicators into a row of distinct (nominal) labelled Variables select
How to convert a row of binary indicators into a row of distinct (nominal) labelled Variables select

Time:12-29

I have a set of candidate regressors (aka Independent Variables or 'IVs') which has been selected by a LASSO Regression I just ran in the following form:

True_IVs
  X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12 X13 X14 X15 X16 X17 X18 X19 X20
1  0  0  0  1  0  0  0  0  0   1   0   0   1   0   0   0   0   0   1   1
  X21 X22 X23 X24 X25 X26 X27 X28 X29 X30
1   0   1   0   0   1   1   1   1   0   1

dput(True_IVs)
structure(list(X1 = "0", X2 = "0", X3 = "0", X4 = "1", X5 = "0", 
    X6 = "0", X7 = "0", X8 = "0", X9 = "0", X10 = "1", X11 = "0", 
    X12 = "0", X13 = "1", X14 = "0", X15 = "0", X16 = "0", X17 = "0", 
    X18 = "0", X19 = "1", X20 = "1", X21 = "0", X22 = "1", X23 = "0", 
    X24 = "0", X25 = "1", X26 = "1", X27 = "1", X28 = "1", X29 = "0", 
    X30 = "1"), row.names = 1L, class = "data.frame")

Where True_IVs is a "data.frame" and its structure is:

'data.frame':   1 obs. of  30 variables:
 $ X1 : chr "0"
 $ X2 : chr "0"
 $ X3 : chr "0"
 $ X4 : chr "1"
 $ X5 : chr "0"
 $ X6 : chr "0"
 $ X7 : chr "0"
.
.
.
 $ X27: chr "1"
 $ X28: chr "1"
 $ X29: chr "0"
 $ X30: chr "1"

And I would like to convert this output into something equivalent to whatever will produce the following output in the Console to facilitate the scoring of these selections:

True_Regressors
[1] "X4" "X10" "X13" "X19" "X20" "X22" "X25" "26" "X27" "X28" "30"

Just to clarify, the above is not actual code but psuedo code of the form I wish to create.

So far, all I have tried is what is below with the corresponding output in the Console:

 True_Regressors <- do.call(
   rbind.data.frame,
   lapply(
   paste("X", True_IVs),
     function(x) {
       s <- paste(x, "")
       c(s[[1]], s[[2]]) } ))

#Error in s[[2]] : subscript out of bounds

CodePudding user response:

You can simply do:

names(True_IVs)[True_IVs == 1]
  • Related