Announcement

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

  • appending frames

    So, I find myself using frames A LOT with Stata 16 to store results and manipulate them and other things. In doing so, I find myself often pining for a "-frame append-" command that would allow me to add the contents of one frame to the bottom of another frame. The ways I do this in the absence of such a command all seem very awkward and inefficient. Does anyone know of a parsimonious way of doing this?

  • #2
    I don't have an answer. I'm responding only to echo the sentiment. Frames are wonderful, but the absence of a -frame append- command is a perplexing and serious omission. I hope the developers at StataCorp are planning to fill this gap soon.

    Comment


    • #3
      It bugged me enough that I wrote a program to do it just now. I'll paste it below. Improvements welcome.

      (Intended behavior: -frameappend framename- will post the frame framename to the bottom of the current frame.)

      Code:
      program define frameappend
      
          syntax namelist(name=frame_name max=1) [, drop]
          
          ** get lists of variables to be combined
          quietly ds
          local to_varlist "`r(varlist)'"
          
          quietly frame `frame_name': ds
          local from_varlist "`r(varlist)'"
          
          local shared_varlist : list from_varlist & to_varlist    
          local new_varlist : list from_varlist - shared_varlist
      
          * get size of new dataframe
          frame `frame_name' : local from_N = _N
          local to_N = _N
          local from_start = `to_N' + 1
          local new_N = `to_N' + `from_N'
      
          qui set obs `new_N'
          qui gen _temp_n = _n
          frame `frame_name' {
              qui gen _temp_n = _n + `to_N'
          }
          
          qui frlink 1:1 _temp_n, frame(`frame_name')
          
          if "`shared_varlist'" != "" {
              qui frget `shared_varlist', from(`frame_name') prefix(_f_)
          
              foreach var of varlist `shared_varlist' {
                  qui replace `var' = _f_`var' in `from_start' / `new_N'
                  qui drop _f_`var'
              }
          }
          
          if "`new_varlist'" != "" {
              qui frget `new_varlist', from(`frame_name')
          }
          
          qui drop _temp_n
          frame `frame_name': drop _temp_n
          qui drop `frame_name'
          if "`drop'" == "drop" {
              qui frame drop `frame_name'
          }
          
      end

      Comment


      • #4
        Spelling out

        Code:
        version 16
        in your program is a key detail -- to cut down on the number of puzzled queries.

        Comment


        • #5
          ...find myself often pining for a "-frame append-" command that would allow me to add the contents of one frame to the bottom of another frame. The ways I do this in the absence of such a command all seem very awkward and inefficient. Does anyone know of a parsimonious way of doing this?
          You can always revert to temporary files. 3 lines max

          Code:
          frame whatever: tempfile whatever
          frame whatever: save `whatever'
          frame default: append using `whatever'
          Last edited by Andrew Musau; 14 Sep 2019, 02:57.

          Comment


          • #6
            Thanks, Jeremy. FYI There was a request for this functionality at the UK Stata Conference earlier this month. BTW thanks for putting -frameappend- on SSC. However, I think there's a glitch with SSC. ssc d frameappend shows files for -osort- (sic). And ssc install frameappend yields "nothing to install"

            Comment


            • #7
              Originally posted by Stephen Jenkins View Post
              Thanks, Jeremy. FYI There was a request for this functionality at the UK Stata Conference earlier this month. BTW thanks for putting -frameappend- on SSC. However, I think there's a glitch with SSC. ssc d frameappend shows files for -osort- (sic). And ssc install frameappend yields "nothing to install"
              Thanks. I e-mailed Kit about this and it is fixed, so ssc install frameappend should now work. (And, yes, I did add -version 16- to it before uploading to ssc!)

              Comment


              • #8
                Many thanks Jeremy. It was me who raised the issue of appending frames at the UK Stata Confreence. I have downloaded your frameappend package and will try it out.

                Comment


                • #9
                  Another option might be to use frame post rather than frlink and frget.
                  Doug Hemken
                  SSCC, Univ. of Wisc.-Madison

                  Comment


                  • #10
                    My little toy example looks like this:
                    Code:
                    clear all
                    
                    sysuse auto
                    
                    frame copy default fulldata
                    frame fulldata:  summarize
                    
                    frame put if foreign, into(someobs)
                    frame someobs:  summarize
                    
                    frame someobs:  frame put mpg weight displacement, into(somevars)
                    frame somevars:  summarize
                    
                    preserve
                    keep if foreign==0
                    
                    forvalues i = 1/`=_N' {
                        frame post somevars (mpg[`i']) (weight[`i']) (displacement[`i'])
                    }
                    restore
                    summarize mpg weight displacement
                    
                    frame somevars:  summarize
                    Doug Hemken
                    SSCC, Univ. of Wisc.-Madison

                    Comment


                    • #11
                      I am currently using frames a lot and needed to append frames, which for I have used Jeremy's module "frameappend" (thanks for it!).

                      I have a lot of observations and I encountered an issue due to the fact that _temp_n is defined as a float variable (default format) and some values above 16777216 cannot be represented exactly with a float (e.g. 16777217 becomes 16777216, see for instance https://www.stata.com/support/faqs/d...oat-data-type/). Because of it, I am ending up having duplicates in _temp_n and appending frames do not work anymore (1:1 mapping is no more possible).

                      I am relatively new to Stata and I don't know if that's the best solution, but I have re-defined _tem_n to be "long" and it then worked fine.

                      Comment

                      Working...
                      X