Announcement

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

  • change matrix column names with the values of a string variable

    Dear all,

    This is similar to what I posted before, but please accept my apology if this was posted somewhere in this forum.


    I have a mtrix
    Code:
    mat A= J(1,216,1)
    mat list A

    I want to replace the column names with the values in this variable. For example, the name of column 1 will be Appleton, the name of colun 2 will be Beechwood

    I tried this code but didn't work.
    Code:
    foreach j=1/216 {
    local var= geography_1[`j']
    
    
    
    matrix colnames A= `var'
    }
    .


    My data is as follows.
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str12 geography_1
    "Appleton"    
    "Beechwood"  
    "Birchfield"  
    "Broadheath"  
    "Daresbury"  
    "Ditton"      
    "Farnworth"  
    "Grange"      
    "Hale"        
    "HaltonBrook"
    "HaltonCastle"
    "HaltonLea"  
    "HaltonView"  
    "Heath"      
    "HoughGreen"  
    "Kingsway"    
    "Mersey"      
    "NortonNorth"
    "NortonSouth"
    "Riverside"  
    "WindmillHill"
    "Appleton"    
    "BewseyandWhi"
    "Birchwood"  
    "Burtonwoodan"
    "Culcheth,Gla"
    "Fairfieldand"
    "Grappenhalla"
    "GreatSankeyN"
    "GreatSankeyS"
    "Hatton,Stret"
    "LatchfordEas"
    "LatchfordWes"
    "Lymm"        
    "Orford"      
    "PenkethandCu"
    "PoplarsandHu"
    "PoultonNorth"
    "PoultonSouth"
    "RixtonandWoo"
    "StocktonHeat"
    "Westbrook"  
    "WhittleHall"
    "Anfield"    
    "BelleVale"  
    "Central"    
    "Childwall"  
    "Church"      
    "Clubmoor"    
    "County"      
    "Cressington"
    "Croxteth"    
    "Everton"    
    "Fazakerley"  
    "Greenbank"  
    "Kensingtonan"
    "Kirkdale"    
    "KnottyAsh"  
    "MossleyHill"
    "NorrisGreen"
    "OldSwan"    
    "Picton"      
    "PrincesPark"
    "Riverside"  
    "Speke-Garsto"
    "StMichael's"
    "TuebrookandS"
    "Warbreck"    
    "Wavertree"  
    "WestDerby"  
    "Woolton"    
    "YewTree"    
    "Arboretum"  
    "Blagreaves"  
    "Boulton"    
    "Chaddesden"  
    "Chellaston"  
    "Darley"      
    "Derwent"    
    "Littleover"  
    "Mackworth"  
    "Mickleover"  
    "Normanton"  
    "Oakwood"    
    "Sinfin"      
    "Spondon"    
    "Abbey"      
    "Aylestone"  
    "BeaumontLeys"
    "Belgrave"    
    "BraunstonePa"
    "Castle"      
    "Charnwood"  
    "Coleman"    
    "Evington"    
    "EyresMonsell"
    "Fosse"      
    "Freemen"    
    "Humberstonea"
    "Knighton"    
    "Latimer"    
    "NewParks"    
    "RusheyMead"  
    "SpinneyHills"
    "Stoneygate"  
    "Thurncourt"  
    "Westcotes"  
    "WesternPark"
    "Arboretum"  
    "Aspley"      
    "Basford"    
    "Berridge"    
    "Bestwood"    
    "Bilborough"  
    "Bridge"      
    "Bulwell"    
    "BulwellFores"
    "CliftonNorth"
    "CliftonSouth"
    "Dales"      
    "DunkirkandLe"
    "LeenValley"  
    "Mapperley"  
    "RadfordandPa"
    "Sherwood"    
    "StAnn's"    
    "WollatonEast"
    "WollatonWest"
    "AbbeyHultona"
    "Baddeley,Mil"
    "BentileeandU"
    "BirchesHeada"
    "BlurtonEast"
    "BlurtonWesta"
    "BoothenandOa"
    "BradeleyandC"
    "BroadwayandL"
    "BurslemCentr"
    "BurslemPark"
    "DresdenandFl"
    "EatonPark"  
    "EtruriaandHa"
    "FentonEast"  
    "FentonWestan"
    "FordGreenand"
    "Goldenhillan"
    "GreatChellan"
    "HanfordandTr"
    "HanleyParkan"
    "Hartshilland"
    "Hollybushand"
    "Joiner'sSqua"
    "LightwoodNor"
    "LittleChella"
    "MeirHay"    
    "MeirNorth"  
    "MeirPark"    
    "MeirSouth"  
    "Moorcroft"  
    "PenkhullandS"
    "SandfordHill"
    "SneydGreen"  
    "Springfields"
    "Tunstall"    
    "WestonCoyney"
    "Alexandra"  
    "Bixley"      
    "Bridge"      
    "CastleHill"  
    "Gainsborough"
    "Gipping"    
    "Holywells"  
    "PrioryHeath"
    "Rushmere"    
    "Sprites"    
    "StJohn's"    
    "StMargaret's"
    "StokePark"  
    "Westgate"    
    "Whitehouse"  
    "Whitton"    
    "Ashley"      
    "Avonmouth"  
    "Bedminster"  
    "Bishopston"  
    "Bishopsworth"
    "BrislingtonE"
    "BrislingtonW"
    "Cabot"      
    "Clifton"    
    "CliftonEast"
    "Cotham"      
    "Easton"      
    "Eastville"  
    "Filwood"    
    "FromeVale"  
    "Hartcliffe"  
    "Henbury"    
    "Hengrove"    
    "Henleaze"    
    "Hillfields"  
    "Horfield"    
    "Kingsweston"
    "Knowle"      
    "LawrenceHill"
    "Lockleaze"  
    "Redland"    
    "StGeorgeEast"
    "StGeorgeWest"
    "Southmead"  
    "Southville"  
    "Stockwood"  
    "StokeBishop"
    "Westbury-on-"
    "WhitchurchPa"
    "WindmillHill"
    end

    Any help would be appreciated.

    Kind regards,

    Kim
    Last edited by sungwook kim; 21 Nov 2018, 09:17.

  • #2
    Code:
    levelsof geography_1, local(colnames)
    mat A= J(1,216,1)
    mat colnames A= `colnames'
    mat list A

    Comment


    • #3
      Dear Andrew,

      Again, thank you very much for your kind& prompt reply. It works great as before.

      Kind regards,

      Kim

      Comment


      • #4
        sungwook kim, levelsof sorts alphabetically and it appeared at first that your observations were sorted in this way. However, looking more closely, this is not the case. If you want to maintain the order, use the following:

        Code:
        gen order=_n
        sum order
        forvalues i = 1/`r(max)'{
        local colnames `colnames' `"`=geography_1[`i']'"'
        }
        mat A= J(1,216,1)
        mat colnames A= `colnames'
        mat list A

        Comment


        • #5
          Dear Andrew,

          I also realised that levelsof sort aphabetically and it changed the result. So, I changed my data alphabetically to use levelsof. Your another solution here is perfect for that case.

          Thank you very much.

          Regards,

          Kim

          Comment

          Working...
          X