Announcement

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

  • Make subset dataset in mata space but I could not find any help about it.

    Dear all.

    I'm trying to make subset dataset in mata space but I could not find any help about it.

    for example..

    If I have a list of variables of (year x1 x2 x3) and I would like to make subset matrix by "year".

    So if year==1990, I would like to make matrix "mat" with all the variables which is in 1990.

    mata:
    st_view(xx=.,.,("year","x1",x2","x3"))

    if (xx[.,1]==1990) {

    mat=xx[.,.]

    }

    The code presented above doesn't work.

    Does anybody can help me??

    Thanks a lot.

  • #2
    ha lee the if condition should be a scalar. Then


    Code:
    x[.,.]
    returns the whole matrix

    There are two solutions

    1. use select() or st_select (see the help for more details)
    2. use st_view with a predefined variable selecting the right year. This variable is passed to st_view() as an additional argument

    The first solution is most probably the one you want

    Code:
    mat = select(xx,xx[,1]:==1990) // note the use of the colon operator for doing element by element operations
    or

    Code:
    st_select(mat,xx,xx[:,1]:==1990)
    In this case since xx is a view, mat will also be a view.

    Another way is to directly specify mat with st_view(). Mat will be a view.

    Code:
    gen d1990 = year == 1990
    // the third argument select all the rows from your variables where d1990 is different from 0
    mata: st_view(mat=.,"year x1 x2 x3","d1990" )
    Last edited by Christophe Kolodziejczyk; 23 Jul 2017, 06:12.

    Comment


    • #3
      Thank you Christophe .

      Comment

      Working...
      X