Announcement

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

  • Convert from "matrix" form to a long format

    Hello everybody,

    I'm new on this forum and I Wonder if I'm in the right topic… I'm using data in a matrix-like form that represent the time required for public transport between two statistical sectors.
    Here is what my data looks like (Listed 100 out of 446 observations) :

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str9 A str17(B C) str16 D str17(E F G H I J K L)
    ""          "21001A011"         "21001A02-"         "21001A041"        "21001A07-"         "21001A10-"         "21001A120"         "21001A152"         "21001A30-"         "21001A32-"        "21001A34-"         "21001A3MJ"      
    "21001A011" ""                  "8.775"             "7.79"             "9.039999999999999" "6.815"             "11.9566666666667"  "13.36"             "10.82"             "14.49"            "14.8833333333333"  "14.76"          
    "21001A02-" "8.52"              "2.2"               "10.34"            "13.87"             "4.35"              "9.30833333333333"  "8.505000000000001" "13.895"            "11.96"            "19.4"              "19.59"          
    "21001A041" "7.89"              "12.88"             "4.545"            "14.545"            "5.635"             "9.07833333333333"  "10.645"            "10.525"            "5.305"            "14.5883333333333"  "14.465"          
    "21001A07-" "8.65"              "15.025"            "13.93"            ""                  "12.675"            "14.53"             "17.83"             "16.97"             "19.42"            "21.0333333333333"  "20.91"          
    "21001A10-" "6.36"              "5.2775"            "5.6575"           "13.08"             "2.505"             "6.65666666666667"  "5.16"              "11.455"            "7.865"            "15.385"            "15.445"          
    "21001A120" "10.81"             "8.49833333333333"  "11.5383333333333" "15.16"             "9.685000000000001" "9.785"             "13.9566666666667"  "15.8766666666667"  "13.7366666666667" "19.9466666666667"  "19.8166666666667"
    "21001A152" "12.36"             "6.705"             "11.38"            "16.45"             "6.83"              "12.24"             ""                  "16.79"             "13.33"            "21.06"             "20.64"          
    "21001A30-" "10.88"             "13.955"            "10.53"            "17.45"             "11.36"             "15.5966666666667"  "17.16"             ""                  "16.29"            "12.7933333333333"  "10.07"          
    "21001A32-" "13.94"             "12.86"             "4.77"             "19.73"             "7.695"             "11.2066666666667"  "12.68"             "16.2"              ""                 "20.22"             "20.19"          
    "21001A34-" "14.8533333333333"  "19.2083333333333"  "14.5033333333333" "21.4266666666667"  "15.1166666666667"  "19.5944444444444"  "20.0333333333333"  "9.356666666666669" "19.0066666666667" "15.4633333333333"  "14.28"          
    "21001A3MJ" "14.86"             "19.69"             "14.505"           "21.43"             "15.38"             "19.5766666666667"  "20.99"             "9.25"              "20.06"            "9.890000000000001" ""                
    "21001A41-" "15.73"             "10.325"            "16.42"            "19.96"             "10.0225"           "14.3066666666667"  "6.12"              "18.09"             "16.87"            "22.2566666666667"  "22.125"          
    "21001A43-" "13.775"            "9.115"             "10.6225"          "19.975"            "6.01"              "13.2583333333333"  "10.865"            "11.66"             "12.68"            "17.745"            "15.685"          
    "21001A441" "6.7"               "9.775"             "6.35"             "13.27"             "7.18"              "11.4166666666667"  "13.35"             "6.93"              "12.08"            "10.1966666666667"  "8.970000000000001"
    "21001A451" "7.88"              "10.955"            "7.53"             "14.45"             "8.359999999999999" "12.5966666666667"  "14.53"             "7.86"              "13.26"            "18.7566666666667"  "9.4"            
    "21001A492" "16.815"            "11.435"            "12.69"            "23.055"            "8.33"              "15.315"            "13.235"            "16.34"             "14.995"           "22.3566666666667"  "20.165"          
    "21001A52-" "13.48"             "8.074999999999999" "12.68"            "17.71"             "8.025"             "12.0566666666667"  "3.87"              "11.93"             "15.22"            "17.86"             "16"              
    "21001A552" "9.279999999999999" "12.355"            "8.93"             "15.85"             "9.76"              "13.9966666666667"  "15.93"             "9.26"              "14.66"            "10.9233333333333"  "16.08"          
    "21001A712" "3.78"              "10.3"              "9.07"             "5.5"               "7.485"             "9.643333333333331" "13.27"             "12.1"              "14.55"            "16.1633333333333"  "16.04"          
    "21001A732" "12.28"             "13.6516666666667"  "16.7416666666667" "13.76"             "14.2166666666667"  "9.42"              "20.1566666666667"  "22.0533333333333"  "19.0466666666667" "26.5177777777778"  "26.0233333333333"
    "21001A74-" "10.32"             "15.75"             "15.765"           "9.539999999999999" "13.855"            "12.7933333333333"  "19.56"             "18.64"             "20.57"            "22.7033333333333"  "22.58"          
    "21001A80-" "17.53"             "17.5125"           "19.205"           "18.19"             "15.9"              "17.3866666666667"  "12.225"            "13.82"             "22.055"           "22.2333333333333"  "17.89"          
    "21001A81-" "18.52"             "16.855"            "19.58"            "16.5"              "16.42"             "17.9633333333333"  "12.65"             "15.98"             "21.61"            "24.47"             "20.05"          
    "21001A82-" "24.24"             "18.835"            "23.055"           "23.29"             "18.4"              "22.7266666666667"  "14.63"             "23.15"             "24.83"            "29.5833333333333"  "26.96"          
    "21001A911" "9.880000000000001" "16.315"            "15.185"           "7.32"              "13.67"             "15.2233333333333"  "19.71"             "18.2"              "20.92"            "22.2633333333333"  "22.14"          
    "21001A941" "8.970000000000001" "10.385"            "13.475"           "16.74"             "10.95"             "6.15333333333333"  "16.82"             "20.43"             "15.78"            "24.4933333333333"  "24.37"          
    "21001A95-" "14.49"             "18.535"            "19.63"            "13.71"             "17.75"             "13.7066666666667"  "17.73"             "19.47"             "22.4"             "25.7866666666667"  "23.48"          
    "21001B10-" "17.62"             "14.385"            "23.175"           "9.16"              "17.27"             "21.5233333333333"  "19.31"             "25.12"             "25.93"            "29.1833333333333"  "29.06"          
    "21001B17-" "9.390000000000001" "9.185000000000001" "14.67"            "3.06"              "12.07"             "15.5033333333333"  "14.11"             "17.7"              "18.51"            "21.7733333333333"  "21.65"          
    "21001B20-" "16.265"            "9.845000000000001" "20.04"            "10.22"             "12.95"             "17.2516666666667"  "18.34"             "23.135"            "20.715"           "26.955"            "27.36"          
    "21001B241" "15.53"             "17.72"             "20.305"           "7.79"              "20.295"            "23.9066666666667"  "24.56"             "24.37"             "19.05"            "28.4333333333333"  "28.31"          
    "21001B321" "11.89"             "5.47"              "16.425"           "10.85"             "8.455"             "12.86"             "12.46"             "18"                "16.27"            "22.0633333333333"  "21.94"          
    "21001B372" "22.06"             "22.1775"           "20.215"           "14.01"             "22.4225"           "25.63"             "27.155"            "27.62"             "18.83"            "31.93"             "31.925"          
    "21001B3MJ" "20.86875"          "20.20625"          "12.9875"          "19.27875"          "15.53"             "18.34375"          "20.51"             "23.17375"          "12.785"           "25.895"            "24.7325"        
    "21001C512" "10.68"             "13.755"            "10.33"            "17.25"             "11.16"             "15.3966666666667"  "17.33"             "10.66"             "16.06"            "12.3233333333333"  "9.48"            
    "21001C5MA" "12.33"             "15.405"            "11.98"            "18.9"              "12.81"             "17.0466666666667"  "18.98"             "12.31"             "17.71"            "13.9733333333333"  "11.13"          
    "21001C611" "18.46"             "13.055"            "17.275"           "20.985"            "12.62"             "17.0333333333333"  "8.85"              "19.595"            "19.05"            "24.5733333333333"  "23.585"          
    "21001C6PB" "18.23"             "12.85"             "14.105"           "24.47"             "9.744999999999999" "16.73"             "14.66"             "17.77"             "16.41"            "23.76"             "21.51"          
    "21001C71-" "23.125"            "19.505"            "22.67"            "25.88"             "18.3525"           "23.5516666666667"  "14.535"            "19.02"             "24.9"             "26.2566666666667"  "23.09"          
    "21002A00-" "35.39"             "41.985"            "40.715"           "38.58"             "39.095"            "41.3966666666667"  "45.02"             "43.71"             "46.4"             "47.7733333333333"  "47.65"          
    "21002A01-" "42.21"             "50.765"            "47.605"           "47.35"             "45.95"             "48.31"             "52.35"             "50.72"             "53.88"            "54.82"             "54.69"          
    "21002A02-" "37.67"             "44.21"             "42.985"           "39.06"             "41.34"             "43.6566666666667"  "47.28"             "45.99"             "48.64"            "50.0533333333333"  "49.94"          
    "21002A041" "30.01"             "36.53"             "35.33"            "31.17"             "33.715"            "36.0166666666667"  "39.56"             "38.33"             "41.02"            "42.3933333333333"  "42.27"          
    "21002A072" "28.31"             "34.83"             "33.63"            "29.47"             "32.015"            "34.3166666666667"  "37.8"              "36.63"             "39.32"            "40.6933333333333"  "40.57"          
    "21002A091" "43.73"             "50.58"             "49.02"            "40.69"             "47.45"             "49.68"             "53.32"             "52.05"             "55"               "56.12"             "56.01"          
    "21002A10-" "38.19"             "44.155"            "43.39"            "38.48"             "41.18"             "44.0433333333333"  "47.71"             "46.51"             "49.05"            "50.5733333333333"  "50.46"          
    "21002A11-" "38.195"            "44.8175"           "43.5175"          "39.3875"           "41.9075"           "44.2016666666667"  "47.8025"           "46.52"             "49.1625"          "50.58"             "50.4825"        
    "21002A12-" "45.8"              "52.515"            "51.125"           "47.15"             "49.505"            "51.8066666666667"  "55.46"             "54.14"             "56.8"             "58.19"             "58.09"          
    "21002A15-" "29.58"             "36.1"              "34.9"             "30.74"             "33.285"            "35.5866666666667"  "39.13"             "37.9"              "40.59"            "41.9633333333333"  "41.84"          
    "21002A20-" "30.34"             "36.935"            "35.66"            "31.5"              "34.045"            "36.3466666666667"  "39.97"             "38.66"             "41.35"            "42.7233333333333"  "42.6"            
    "21002A21-" "26.28"             "32.7775"           "31.5975"          "27.38"             "29.9675"           "32.2816666666667"  "35.795"            "34.6"              "37.19"            "38.6633333333333"  "38.54"          
    "21002A24-" "33.44"             "40.455"            "38.765"           "36.25"             "37.145"            "39.45"             "42.95"             "41.76"             "44.58"            "45.8233333333333"  "45.71"          
    "21002A25-" "36.495"            "43.3175"           "41.7725"          "38.615"            "40.105"            "42.4316666666667"  "46.16"             "44.83"             "47.605"           "48.8966666666667"  "48.8"            
    "21002A372" "31.01"             "37.9"              "36.345"           "32.32"             "34.625"            "37.01"             "40.65"             "39.36"             "41.99"            "43.4233333333333"  "43.3"            
    "21002A411" "44.51"             "53.105"            "49.905"           "49.69"             "48.25"             "50.61"             "54.65"             "53.04"             "56.19"            "57.12"             "56.99"          
    "21002A422" "42.95"             "49.8"              "48.24"            "39.91"             "46.67"             "48.9033333333333"  "52.54"             "51.27"             "54.22"            "55.34"             "55.23"          
    "21002A43-" "37.46"             "44.055"            "42.785"           "40.65"             "41.21"             "43.4666666666667"  "47.09"             "45.78"             "48.47"            "49.8433333333333"  "49.79"          
    "21002A511" "30.795"            "37.3525"           "36.1175"          "31.955"            "34.5225"           "36.8016666666667"  "40.355"            "39.115"            "41.735"           "43.1783333333333"  "43.09"          
    "21002A52-" "33.815"            "40.4025"           "39.14"            "34.965"            "37.535"            "39.8216666666667"  "43.445"            "42.135"            "44.815"           "46.1983333333333"  "46.11"          
    "21002A53-" "34.63"             "41.225"            "40"               "35.79"             "38.335"            "40.7"              "44.34"             "43.06"             "45.72"            "47.1233333333333"  "47"              
    "21002A572" "25.34"             "31.86"             "30.66"            "26.42"             "29.045"            "31.3466666666667"  "34.83"             "33.66"             "36.21"            "37.7233333333333"  "37.6"            
    "21003A00-" "25.78"             "34.505"            "32.07"            "26.055"            "30.405"            "31.9133333333333"  "37.045"            "34.34"             "41.64"            "38.4033333333333"  "38.28"          
    "21003A011" "20.05"             "28.48"             "26.19"            "20.17"             "24.56"             "26.3833333333333"  "30.6"              "28.37"             "37.29"            "32.4333333333333"  "32.31"          
    "21003A02-" "20.185"            "28.4425"           "27.405"           "18.475"            "25.7325"           "26.2266666666667"  "31.925"            "28.505"            "34.695"           "32.5683333333333"  "32.445"          
    "21003A05-" "24.02"             "33.935"            "31.24"            "21.8"              "29.58"             "30.87"             "36.49"             "32.34"             "39.47"            "36.4033333333333"  "36.28"          
    "21003A0AJ" "31.08"             "39.48"             "37.175"           "30.86"             "35.49"             "36.26"             "42.09"             "39.4"              "44.41"            "43.4633333333333"  "43.34"          
    "21003A10-" "20.36"             "29.875"            "29.415"           "16.86"             "27.715"            "26.7666666666667"  "33.44"             "28.68"             "35.59"            "32.7433333333333"  "32.62"          
    "21003A212" "25.82"             "34.57"             "31.96"            "25.94"             "30.33"             "31.5366666666667"  "37.11"             "34.14"             "39.83"            "38.25"             "38.15"          
    "21003A2MJ" "29.615"            "38.47"             "35.88"            "29.32"             "34.2025"           "35.4183333333333"  "40.99"             "37.96"             "43.65"            "42.0583333333333"  "41.965"          
    "21003A312" "27.0033333333333"  "34.6166666666667"  "33.3883333333333" "25.5833333333333"  "31.7966666666667"  "31.7188888888889"  "37.8"              "35.3066666666667"  "40.1666666666667" "39.37"             "39.2466666666667"
    "21003A323" "27.42"             "36.3975"           "34.6275"          "25.53"             "32.655"            "33.4033333333333"  "39.235"            "35.74"             "42.005"           "39.8033333333333"  "39.68"          
    "21003A331" "25.09"             "34.915"            "32.04"            "25.8"              "29.6"              "32.3166666666667"  "37.41"             "33.41"             "40.79"            "37.4733333333333"  "37.35"          
    "21003A38-" "24.7"              "31.23"             "30.59"            "23.44"             "29.18"             "28.4533333333333"  "35.12"             "33.02"             "36.47"            "37.0833333333333"  "36.96"          
    "21004A001" "21.13"             "27.505"            "26.94"            "18.03"             "25.385"            "28.0133333333333"  "33.09"             "30.34"             "30.06"            "34.4033333333333"  "34.28"          
    "21004A002" "13.58"             "20.98"             "19.565"           "11.49"             "18.285"            "21.6733333333333"  "25.89"             "23.3"              "22.31"            "27.8133333333333"  "27.69"          
    "21004A01-" "20.6"              "27.77"             "26.665"           "19.86"             "25.02"             "27.4733333333333"  "32.17"             "29.68"             "30.68"            "33.8833333333333"  "33.76"          
    "21004A02-" "20.49"             "26.17"             "26.16"            "17.61"             "26.515"            "28.5333333333333"  "33.91"             "29.63"             "27.5"             "33.6933333333333"  "33.57"          
    "21004A04-" "24.46"             "27.17"             "28.745"           "16.3"              "29.305"            "31.91"             "31.85"             "33.59"             "27.91"            "37.6866666666667"  "37.53"          
    "21004A10-" "18.41"             "24.3175"           "23.965"           "15.5"              "22.05"             "24.825"            "27.95"             "27.53"             "29.09"            "31.7116666666667"  "31.625"          
    "21004A13-" "17.31"             "20.27"             "22.5916666666667" "10.3433333333333"  "21.66"             "23.8977777777778"  "23.8766666666667"  "25.66"             "24.26"            "29.7455555555556"  "29.6"            
    "21004A14-" "21.835"            "27.37"             "27.5975"          "18.18"             "25.59"             "28.415"            "31.38"             "30.675"            "31.965"           "34.7533333333333"  "34.615"          
    "21004A15-" "13.45"             "17.02"             "18.72"            "7.12"              "18.165"            "20.3766666666667"  "19.8"              "21.77"             "20"               "25.8333333333333"  "25.71"          
    "21004A16-" "18.51"             "21.1"              "23.29"            "11.19"             "22.735"            "25.1866666666667"  "23.87"             "26.83"             "24.08"            "30.8933333333333"  "30.77"          
    "21004A19-" "16.65"             "22.995"            "22.1625"          "15.425"            "20.355"            "22.8966666666667"  "26.6"              "25.445"            "27.905"           "30.0216666666667"  "29.89"          
    "21004A1MJ" "12.99"             "19.51"             "18.275"           "13.21"             "16.695"            "18.87"             "23.31"             "21.31"             "24.62"            "25.9866666666667"  "26.17"          
    "21004A21-" "16.83"             "20.29"             "21.6066666666667" "12.23"             "20.7025"           "24.0794444444444"  "24.6316666666667"  "25.7416666666667"  "22.9383333333333" "29.8205555555556"  "29.6816666666667"
    "21004A22-" "15.15"             "21.83"             "20.95"            "12.93"             "19.895"            "22.9366666666667"  "23.98"             "23.47"             "24.64"            "27.5333333333333"  "27.41"          
    "21004A23-" "15.43"             "24.03"             "21.3225"          "16.675"            "19.1525"           "22.02"             "26.17"             "24.555"            "27.585"           "28.6183333333333"  "28.495"          
    "21004A24-" "13.995"            "21.515"            "19.585"           "14.575"            "18.1625"           "20.4566666666667"  "24.34"             "22.6"              "25.455"           "26.8966666666667"  "26.54"          
    "21004A32-" "16.2"              "24.4"              "21.835"           "16.45"             "19.865"            "22.56"             "26.33"             "25.23"             "26.97"            "29.2933333333333"  "29.17"          
    "21004A33-" "20.3833333333333"  "26.05"             "25.9133333333333" "16.6233333333333"  "24.24"             "26.9644444444444"  "29.5"              "29.16"             "30.2933333333333" "33.2566666666667"  "33.1"            
    "21004A34-" "11.49"             "19.72"             "16.775"           "10.96"             "15.195"            "17.37"             "21.7"              "19.81"             "22.25"            "24.42"             "24.57"          
    "21004A3MJ" "18.35"             "25.005"            "24.055"           "19.61"             "22.005"            "25.1233333333333"  "28.93"             "27.54"             "30.17"            "31.6033333333333"  "31.48"          
    "21004A70-" "15.94"             "19.59"             "20.8925"          "8.695"             "21.215"            "23.6983333333333"  "23.455"            "24.435"            "21.415"           "28.545"            "28.375"          
    "21004A71-" "20.84"             "25.075"            "26.085"           "14.51"             "26.415"            "28.75"             "29.49"             "29.16"             "26.8"             "33.5966666666667"  "33.41"          
    "21004A822" "18.47"             "27.115"            "24.545"           "13.92"             "23.95"             "25.31"             "28.48"             "26.79"             "29.95"            "30.8533333333333"  "30.73"          
    "21004B10-" "19.32"             "24.57625"          "24.63"            "14.845"            "23.20625"          "25.4791666666667"  "27.9875"           "27.67"             "28.8875"          "31.7366666666667"  "31.6075"        
    "21004B13-" "20.1433333333333"  "26.435"            "25.4516666666667" "17.9066666666667"  "23.8483333333333"  "26.1"              "29.5433333333333"  "28.4633333333333"  "30.81"            "32.5288888888889"  "32.4033333333333"
    "21004B1MJ" "16.78"             "23.3"              "22.065"           "15.71"             "20.485"            "22.7133333333333"  "26.33"             "25.1"              "27.65"            "29.1633333333333"  "29.04"          
    end
    I would like to be able to convert this data to include it in another database (in long format). More concretely, I need to reshape the data in a way to have 2 columns (and therefore two variables) : one that represents statistical sectors combination (eg.
    21001A011-21001A011) and the other one that contains the time value.

    I hope my questions are clear enough to receive any help !

    Thank you very much and have a good day.
    Last edited by Sarah Bellet; 15 Jan 2019, 04:51.

  • #2
    Welcome to Statalist.

    You have accidentally posted your topic in Statalist's Mata Forum, which is used for discussions of Stata's Mata language. Your question would have seen a much larger audience if you had posted it in Statalist's General Forum. While your data is organized as a matrix, it is standard Stata dataset, not a Mata matrix as described by help mata and the solution uses the Stata reshape command.

    With that said, rather than have you repost in the General Forum, here's some code that should start you toward what you want.
    Code:
    foreach v of varlist B-L {
        local varname = `v'[1]
        local varname = subinstr("`varname'","-","_",.)
        rename `v' V_`varname'
        }
    drop in 1
    destring V_*, replace
    rename A sector1
    generate id = _n
    reshape long V_, i(id) j(sector2) string
    rename V_ value
    replace sector2 = subinstr(sector2,"_","-",.)
    drop id
    order sector1 sector2 value
    list if sector1==sector1[1], clean noobs
    Code:
    . list if sector1==sector1[1], clean noobs
    
          sector1     sector2       value  
        21001A011   21001A011           .  
        21001A011   21001A02-       8.775  
        21001A011   21001A041        7.79  
        21001A011   21001A07-        9.04  
        21001A011   21001A10-       6.815  
        21001A011   21001A120   11.956667  
        21001A011   21001A152       13.36  
        21001A011   21001A30-       10.82  
        21001A011   21001A32-       14.49  
        21001A011   21001A34-   14.883333  
        21001A011   21001A3MJ       14.76

    Comment


    • #3
      Thanks a lot William. This is exactly the code I needed ! Have a nice day

      P.S : I apologize for posting my question in the wrong topic. I Don't know the difference yet between a "standard" matrix and a mata matrix

      Comment


      • #4
        No apology needed, your mistake has been made several times in the past few days, oddly enough. Some of us have questioned the justification for a separate Forum for Mata questions. Myself, I'm interested in Mata so I follow both Forums, but I'd like have a separate forum for questions about the Fama-French technique in finance so I could more easily skip over them.

        Comment

        Working...
        X