Announcement

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

  • Mata: uniqrows() produces error with string matrices

    The documentation says that uniqrows() works with transmorphic matrix P. However, uniqrows(P,1) produces an error when P is a string matrix. For example:

    Code:
    sysuse surface, clear
    mata:
      P = st_sdata(.,"date",.)
      uniqrows(P, 0)
      uniqrows(P, 1)
    end
    The source of the error is obvious from the source code (view uniqrows.mata, adopath asis ). The last line will produce an error if res is a string matrix and count is a real matrix.
    Code:
     if (freq) res = res,count
    Is there a way to calculate the frequencies for string matrices?

  • #2
    The problem is that a Mata matrix can't have both strings and reals, and there is no easy workaround for that.

    What I do is use the ftools package from SSC (I wrote it so I'm of course biased):

    Code:
    mata:
    x = "a" \ "a" \ "b" \ "a" \ "c"
    F = _factor(x)
    F.keys
    F.counts
    end
    If you are importing the data from stata (which you are in the example), then you don't need to use "_factor()" but can use factor("date") directly

    Comment


    • #3
      Thanks - I didn't know about your ftools package, but it looks useful. I did find another word around; I mainly surprised to find that uniqrows() didn't work as documented.

      Comment

      Working...
      X