R language Combat-Advanced data Management (4)

Source: Internet
Author: User

6. Integration and Refactoring

6.1 Transpose

> Mtcars MPG cyl disp hp Drat wt Qsec VS AM gear Carbmazda RX4 21 6 160 110 3.9 2.6 0 1 4 4Mazda RX4 Wag 6 3.9 2.9 0 1 4 4Datsun 710 23 4 108 9   3 3.8 2.3 1 1 4 1Hornet 4 Drive 6 258 3.1 3.2 1 0 3 1Hornet sportabout 19           8 175 3.1 3.4 0 0 3 2Valiant 6 225 1Duster 2.8 3.5 1 0 3 360 8 245 3.2 3.6 0 0 3 4Merc 240D 4 147 3.7 3.2 1 0 4 2Merc     4 141 3.9 3.1 1 0 4 2MERC 280 19 6 168 123 3.9 3.4 18 1 0 4  4MERC 280C 6 168 123 3.9 3.4 1 0 4 4Merc 450SE 16 8 276 180 3.1 4.1 17  0 0 3 3Merc 450SL 8 276 3.1 3.7 0 0 3 3Merc 450SLC 15 8 276 180 3.1    3.8 18 0) 0 33Cadillac Fleetwood 8 472 205 2.9 5.2 0 0 3 4Lincoln Continental 10 8 460 215 3.0 5.4 18 0    0 3 4Chrysler Imperial 8 3.2 5.3 0 0 3 4Fiat 128 32 4 79 66 4.1 2.2 1 1 4 1Honda Civic 4 4.9 1.6 1 1 4 2Toyota Corolla 34 4 71 6   5 4.2 1.8 1 1 4 1Toyota Corona 4 1Dodge 3.7 2.5 1 0 3 Challenger 16           8 318 2.8 3.5 0 0 3 2AMC Javelin 8 304 2Camaro 3.1 3.4 0 0 3 Z28 8 245 3.7 3.8 0 0 3 4Pontiac Firebird 8 2Fiat 175 3.1 3.8 0 0 3     X1-9 4 4.1 1.9 1 1 4 1Porsche 914-2 26 4 120 91 4.4 2.1 17 0 1 5  2Lotus Europa 4 113 3.8 1.5 1 1 5 2Ford Pantera L 16 8 351 264 4.2 3.2 14 0 1 5 4Ferrari DIno 6 145 175 3.6 2.8 0 1 5 6Maserati Bora 15 8 301 335 3.5 3.6 15 0 1 5                  8Volvo 142E 4 121 109 4.1 2.8 1 1 4 2> > > Cars <-mtcars[1:5, 1:4]> cars   MPG cyl disp Hpmazda RX4 6 110Mazda RX4 Wag 6 110Datsun 710 23 4 108 93Hornet 4 Drive 6 258 110Hornet sportabout 8 175> t (Cars) Mazda RX4 Mazda RX4 Wag Dat          Sun 710 Hornet 4 Drive Hornet Sportaboutmpg 19cyl               6 6 4 6 8disp 160 160 108 258  360HP 175>

6.2 Consolidating data

> Options (digits=3) > Attach (mtcars) > AggData <-Aggregate (Mtcars, By=list (cyl,gear), Fun=mean, Na.rm=true) > AggData  group.1 group.2  MPG cyl disp  hp drat   wt qsec  vs   am Gear CARB1       4       3 21.5   4  3.70 2.46 20.0 1.0 0.00    3 1.002       6       3 19.8   6  242 108 2.92 3.34 19.8 1.0 0.00    3 1.003       8       3 15.1   8  358 194 3.12 4.10 17.1 0.0 0.00    3 3.084       4       4 26.9   4  103  4.11 2.38 19.6 1.0 0.75    4 1.505       6       4 19.8   6 164 by  3.91 3.09 17.7 0.5 0.50
   
    4 4.006       4       5 28.2   4  108 102 4.10 1.83 16.8 0.5 1.00    5 2.007       6       5 19.7   6  145 1 3.62 2.77 15.5 0.0 1.00    5 6.008       8       5 15.4   8  326 300 3.88 3.37 14.6 0.0 1.00    
   

6.3 Reshape Bag

> ID <-C (1,1,2,2) > Time <-C (1,2,1,2) > X1 <-C (5,3,6,2) > X2 <-C (6,5,1,4) > MyData <-data.f Rame (ID, time, X1, X2) > MyData  ID time X1 X21  1    1  5  1    2 3  /  2    1  6  2    2  2  4

6.3.1 Fusion

> Library (Reshape) > MD <-Melt (MyData, id= (c ("id", "Time")) > MD  ID time variable value1  1    1< C20/>x1  1    2       X1  2    1       X1  2    2       X1     25  1    1       X2  1    2       X2 2 1       X2    2       2 X2     

6.3.2 Re-casting

> Library (reshape) > MD <-Melt (MyData, id=c ("id", "Time")) > MD ID Time       Variable value1 1 1 x1 1 2 x1 2 1 x1 2 2 x1 25 1 1 X2 1 2 x2 2 1 x2 2 2 x2 4> cast (MD, id~variable, mean) ID x 1 x21 1 4 5.52 2 4 2.5> cast (MD, time~variable, mean) time X1 x21 1 5.5 3.52 2 2.5 4.5> cast (MD, Id~ti    Me, mean) ID 1 1 5.5 2 3.5 3> cast (MD, id+time~variable) ID time x1 x21 1 1 5 62 1 2 3 53 2 1 6 2 2 2 4> cast (MD, id+variable~time) ID variable 1 1 x1 5 1 x2 6 2 x1 6 2       4 2 x2 1 4> cast (MD, id+variable~time) ID variable 1 1 x1 5 + 1 x2 6 2 X1 6 24 2 X2 1 4> CAST (MD, id~variable+time) ID x1_1 x1_2 x2_1 x2_21 1 5 3 6 2 6 2 1 4> 

R language Combat-Advanced data Management (4)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.