Stata:将面板行转置为列

时间:2018-03-26 19:43:22

标签: panel stata

我正在尝试将以下面板数据集重新排列为可以与另一个合并的表单。我想改变这个:

## miniversion of survey
if(!require(shiny))install.packages("shiny");require(shiny)
if(!require(shinyjs)) install.packages("shinyjs"); require(shiny)
if(!require(htmlwidgets)) install.packages("htmlwidgets"); require(htmlwidgets)
if(!require(shinyWidgets)) install.packages("shinyWidgets"); require(shinyWidgets)
if(!require(DT)) install.packages("DT"); require(DT)


answer_options = c("riding", "climbing", "travelling", "binge watching series", "swimming", "reading")

# https://stackoverflow.com/questions/37875078/shiny-checkbox-in-table-in-shiny/37875435#37875435
shinyInput <- function(FUN, ids, ...) {
  inputs <- NULL
  inputs <- sapply(ids, function(x) {
    inputs[x] <- as.character(FUN(inputId = x, label = NULL, ...))
  })
  inputs
}

# 
shinyApp(
  ui = fluidPage( ####
    useShinyjs(),# For Shinyjs functions
    tags$script("
                Shiny.addCustomMessageHandler('resetValue', function(variableName) {
                Shiny.onInputChange(variableName, null);
                });
                "),

    tags$style('{background-color: #256986;}'),

    div(class="content",
        # progressbar showing the progress of the survey, currently moves ahead per page in steps of 
        # 12.5 (excluding intro and thank you page)

        progressBar(id= "pbar", value= 0, size= "xs"),

        # main utput/ modified userinterface for each page
        uiOutput("MainAction")
        )
    ),



  server =function(input, output, session) { ####
    output$MainAction <- renderUI({
      PageLayouts()
    })

    CurrentPage <- reactiveValues(page= "page1",
                                  selected= 0)
    PageLayouts<- reactive({


      if(CurrentPage$page == "page1"){
        return(
          list(
            textInput(inputId = "username", label= "Please enter your username"),

            # button displayed to continue
            div(class= "next button",actionButton(inputId = "p1_next", #input ID refers to following page
                                                  label= "Continue"))
          ))
      }


      if(CurrentPage$page == "page2"){
        checkboxtable2 <- data.frame(
          "answer options" = shinyInput(checkboxInput, answer_options),
          "What are your hobbies?" = answer_options,
          check.names = FALSE
        )

        output$table_p2 <- DT::renderDataTable(
          checkboxtable2,
          server = FALSE, escape = FALSE, selection = 'none',
          rownames = FALSE,
          options = list(
            dom = 't', paging = FALSE, ordering = FALSE,
            columnDefs = list(list(targets= 0, width= '30px')),
            preDrawCallback = JS('function() { Shiny.unbindAll(this.api().table().node()); }'),
            drawCallback = JS('function() { Shiny.bindAll(this.api().table().node()); } '))
        )

        return(
          list(
            # create datatable with checkboxes
            p("What are your hobbies?"),
            # create datatable with checkboxes
            # surpresses header without removing checkboxes
            tags$head(tags$style(type = "text/css", "#table_p2 th {display:none;}")),
            DT::dataTableOutput('table_p2'),

            updateProgressBar(session= session, id= "pbar", value = 12.5),
            tags$style(".progress-bar {background-color: #25c484;}")
          ))

      }
    })

      observeEvent(input$p1_next, {CurrentPage$page <- "page2"})
      })

分为:

Gender  Year  IndA  IndB  IndC
   1    2008  0.22  0.34  0.45
   2    2008  0.78  0.66  0.55
   1    2009  0.25  0.36  0.49
   2    2009  0.75  0.64  0.51
   1    2010  0.28  0.38  0.48
   2    2010  0.72  0.62  0.52

我是Stata的新手,我很难重塑专栏和性别。

3 个答案:

答案 0 :(得分:1)

请参阅help reshape。一种方法是连续重塑。您可以执行第一行,查看数据浏览器中的数据,然后执行第二行以查看其工作原理。您还需要为最终变量选择1和2以外的名称。

    reshape long Ind, i(Year Gender)   j(Industry) string
    reshape wide Ind, i(Year Industry) j(Gender)

答案 1 :(得分:1)

您还可以使用stack替换第一个重塑(不太清晰,但有时可能比reshape更快):

stack Gender Year IndA Gender Year IndB Gender Year IndC, into(Gender Year Y) clear
rename _stack Industry
lab define Industry 1 "A" 2 "B" 3 "C"
lab val Industry Industry

reshape wide Y, i(Industry Year) j(Gender)
sort Industry Year
gen id = _n
order id Year Industry
list, sepby(Industry) noobs

答案 2 :(得分:1)

作为同一主题的第三个变体,请注意两个性别的比例总和为1,因此我们只需要一个。

  clear 
  input Gender  Year  IndA  IndB  IndC
   1    2008  0.22  0.34  0.45
   2    2008  0.78  0.66  0.55
   1    2009  0.25  0.36  0.49
   2    2009  0.75  0.64  0.51
   1    2010  0.28  0.38  0.48
   2    2010  0.72  0.62  0.52
  end 
  drop if Gender == 1 
  drop Gender 
  reshape long Ind , i(Year) j(Type) string 

  list , sepby(Year) 

     +-------------------+
     | Year   Type   Ind |
     |-------------------|
  1. | 2008      A   .78 |
  2. | 2008      B   .66 |
  3. | 2008      C   .55 |
     |-------------------|
  4. | 2009      A   .75 |
  5. | 2009      B   .64 |
  6. | 2009      C   .51 |
     |-------------------|
  7. | 2010      A   .72 |
  8. | 2010      B   .62 |
  9. | 2010      C   .52 |
     +-------------------+