Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • selectindex()

    Trying to put something together to address a bug I introduced into brewscheme. The examples are related to the dataset used by the package (hence the reference). The help for this function reads:

    selectindex(v) returns

    1. a row vector of column indices j for which v[j]!=0 (v a row vector) or

    2. a column vector of row indices i for which v[i]!=0 (v a column vector)

    ...

    selectindex(v)
    v: r1 x 1 or 1 x c1
    result: r2 x 1 or 1 x c2, r2 <= r1, c2 <= c1

    Code:
    // Loads the data set with palettes used by -brewscheme-
    use `"`c(sysdir_personal)'b/brewmeta.dta"', clear
    
    // Starts mata
    mata:
    
    // Gets the string data from the data set 
    : paletteData = st_sdata(., ("palette", "rgb", "achromatopsia", "protanopia", "deuteranopia", "tritanopia"))
    
    // Gets numeric data from the data set
    : colorData = st_data(., ("colorid", "pcolor", "maxcolors"))
    
    // Color palette to use for example functions below
    : paletteName = "set1"
    
    // Gets the indices for the color palette of interest
    : paletteIDs = selectindex(paletteData[., 1] :== paletteName)
    
    // List the matrix indices for this palette
    : paletteIDs
               1
         +--------+
       1 |  1062  |
       2 |  1063  |
       3 |  1064  |
       4 |  1065  |
       5 |  1066  |
       6 |  1067  |
       7 |  1068  |
       8 |  1069  |
       9 |  1070  |
      10 |  1071  |
      11 |  1072  |
      12 |  1073  |
      13 |  1074  |
      14 |  1075  |
      15 |  1076  |
      16 |  1077  |
      17 |  1078  |
      18 |  1079  |
      19 |  1080  |
      20 |  1081  |
      21 |  1082  |
      22 |  1083  |
      23 |  1084  |
      24 |  1085  |
      25 |  1086  |
      26 |  1087  |
      27 |  1088  |
      28 |  1089  |
      29 |  1090  |
      30 |  1091  |
      31 |  1092  |
      32 |  1093  |
      33 |  1094  |
         +--------+
        
    // Gets all the unique combinations of the number of colors
    : ncolors = uniqrows(colorData[paletteIDs, 2])
    
    // Displays all of the values that have valid numbers of colors for the given palette
    : ncolors
           1
        +-----+
      1 |  3  |
      2 |  4  |
      3 |  5  |
      4 |  6  |
      5 |  7  |
      6 |  8  |
        +-----+
    
        
    // Tests if the number of requested colors is in the unique values
    : colorIDs = selectindex(ncolors :== 6)
    
    : colorIDs
      4
    
    : colorIDs = selectindex(ncolors :== 12)
    
    : colorIDs
    
    :
    I wanted to include an additional test/check to make sure the number passed by the end user would select the appropriate subset of records, but it looks like the selectindex() function returns void when the value is not found. I've tried wrapping the result in the missing() function, but that also failed to detect that the variable didn't contain any results. The reason for all of this is that I need analogous functionality to the levelsof command without sorting the values (e.g., need to get the list of RGB strings for a given combination of palette name and colors in the order in which they exist in the data set). Wondering if anyone else had any thoughts on this?

  • #2
    I think you have to test whether the number of columns or rows is zero

    Code:
    . mata :
    ------------------------------------------------- mata (type end to exit) -------------
    : 
    : a = J(10,1,1)
    
    : 
    : // b = .
    : b = select(a,a:==12)
    
    : 
    : b
    
    : cols(b)
      1
    
    : rows(b)
      0
    
    : 
    : end
    ---------------------------------------------------------------------------------------

    Comment


    • #3
      Christophe Kolodziejczyk awesome...

      Code:
      : a = J(10,1,1)
      
      : a
              1
           +-----+
         1 |  1  |
         2 |  1  |
         3 |  1  |
         4 |  1  |
         5 |  1  |
         6 |  1  |
         7 |  1  |
         8 |  1  |
         9 |  1  |
        10 |  1  |
           +-----+
      
      : b = selectindex(a[., 1] :== 12)
      
      : b
      
      : cols(b)
        1
      
      : rows(b)
        0
      I needed it to work with select index first and it does. Basically I'm using this to bounce back and forth between the string and numeric data in the file so I can find the appropriate row indices to select the correct records from the file. This definitely was helpful and I'll end up trying to throw this into something that could generalize a bit more in the not to distant future.

      Comment


      • #4
        I have Stata 12 on my computer and selectindex() is not available so I gave an example with select() instead.

        Comment


        • #5
          Christophe Kolodziejczyk thanks for mentioning that. I hadn't thought about the backwards compatibility issue just yet. It looks like the selectindex() function was introduced in Stata 13 so it will at least be useful for my immediate needs, but when I get a chance to put something together that is a bit more generic in nature it will definitely be something I'll try to remember so it can be useful to more users (e.g., trying to build out something analogous to levelsof but with an option to return the list of values in the order in which they appear in the data).

          Comment

          Working...
          X