The ddply and aggregate of the R language weapon

Source: Internet
Author: User

Ddply and aggregate are two powerful functions for consolidating data.

aggregate (x, ...)

The use of the aggregate () function in the "R language Combat" P105 has a simple description, here to say again. This function mainly has several uses:
  

    


Example:

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.500 4 120.1000 97.0000 3.700000 2.465000 20.0100 1.0 0.00 3 1.0000002 6 3 19.750 6 241.5000 107.5 000 2.920000 3.337500 19.8300 1.0 0.00 3 1.0000003 8 3 15.050 8 357.6167 194.1667 3.120833 4.104083 17.14       25 0.0 0.00 3 3.0833334 4 4 26.925 4 102.6250 76.0000 4.110000 2.378125 19.6125 1.0 0.75 4 1.5000005 6 4 19.750 6 163.8000 116.5000 3.910000 3.093750 17.6700 0.5 0.50 4 4.0000006 4 5 28.200 4 107.7000 102.0000 4.100000 1.826500 16.8000 0.5 1.00 5 2.0000007 6 5 19.700 6 145.0000 175.0000 3.620000    2.770000 15.5000 0.0 1.00 5 6.0000008 8 5 15.400 8 326.0000 299.5000 3.880000 3.370000 14.5500 0.0 1.00 5 6.000000 

Get the data frame AggData, where the column names of GROUP.1 and GROUP.2 can be specified, only the second line is written:

AggData <-aggregate (Mtcars, By=list (group.cyl=cyl, Group.gears=gear), Fun=mean, Na.rm=true)

Can.

Note: When using the aggregate () function, the variables in the by must be in a list (even if there is only one variable). The specified function fun can be any built-in or self-coding function.

Some other examples are:  

# # Compute The averages for the variables in ' state.x77 ', grouped## according to the region (northeast, south, North Centr AL, West) that## each state belongs to.aggregate (state.x77, list (region = state.region), mean) # # Compute the averages Acco Rding to region and the occurrence of more## than-days of frost.aggregate (state.x77, list (region = State.regi On,cold = state.x77[, "Frost"] >), mean) # # (Note that the no state in ' south ' was that Cold.) # # example with character variables and NASTESTDF <-data.frame (v1 = C (1,3,5,7,8,3,5,na,4,5,7,9), V 2 = C (11,33,55,77,88,33,55,na,44,55,77,99)) by1 <-C ("Red", "Blue", 1, 2, NA, "big", 1, 2, "Red", 1, NA,) by2 <-C  ("Wet", "dry", "aggregate", "damp", "TESTDF", "X", "The", "Red", "the", "the", "the", "Na", "NA", "by1", "+"), "(By2, Mean)," " If you want to treat NAs as a groupfby1 <-factor (by1, exclude = "") Fby2 <-factor (by2, exclude = "") Aggregate (x = TESTDF, by = List (fby1, fby2), fun = "mean"# # formulas, one ~ one, one ~ many, many ~ one, and many ~ many:aggregate (weight ~ feed, data = CHICKWTS, mean) aggregate ( Breaks ~ Wool + tension, data = Warpbreaks, mean) aggregate (Cbind (Ozone, Temp) ~ Month, data = airquality, mean) aggregate (c Bind (ncases, Ncontrols) ~ ALCGP + TOBGP, data = Esoph, sum) # # Dot notation:aggregate (. ~ species, data = Iris, mean) Aggreg Ate (len., data = Toothgrowth, mean) # Often followed by Xtabs (): AG <-aggregate (len ~., data = Toothgrowth, mean) XT ABS (len ~., data = AG) # Compute The average annual approval ratings for American Presidents.aggregate (presidents, nfrequ Ency = 1, fun = mean) # Give the summer less weight.aggregate (presidents, nfrequency = 1, fun = Weighted.mean, W = C (1, 1, 0.5, 1))
Ddply

The following is the general use of the Ddply function:

Ddply (. Data,. variables,. Fun = NULL,. Progress = "None",. inform = False,. Drop = TRUE,. Parallel = False,. paropts = NULL)

Cases:

# Summarize a DataSet by variablesdfx <-Data.frame (group = C (Rep (' a ', 8), Rep (' B ', ' d '), Rep (' C ', 6)), sex = Sam    Ple (C ("M", "F"), size = $, replace = TRUE), age = runif (n = +, Min = +, max = Si)) Head (DFX) group sex age1 A  M 22.447502 a M 52.926163 a f 30.004434 a M 39.569075 a m 18.891806 a F 50.81139#note the Use of the '. ' function to allow# group and sex to being used without quotingddply (DFX,. (     Group, sex), Summarize,mean = round (mean (age), 2), SD = round (SD (age), 2)) group sex mean SD1 A F 40.41 14.712 A M 30.35 13.173 b f 34.81 12.764 b M 34.04 13.365 c F 35.09 13.396 C M 28.53 4.57# an Examp Le using a formula for. Variablesddply (baseball[1:100,], ~ year, nrow) year V11 1871 72 1872 133 1873 134 1874 155 1875 1 1876 157 1877 178 1878 3# Applying-functions; Nrow and Ncolddply (baseball,. (   LG), C ("Nrow", "Ncol")) LG Nrow Ncol1-222 AA 171 223 AL 10007 224 FL 37225 NL 11378 226 PL 227 UA 9 22# Calculate mean runs batted in for each Yearrbi <-(ddply,. (  year), Summarise,mean_rbi = mean (RBI, na.rm = TRUE)) Head (RBI) year MEAN_RBI1 1871 22.285712 1872 20.538463 1873 30.923084 1874 29.000005 1875 31.588246 1876 30.13333# Plot A line chart of the Resultplot (Mean_rbi ~ year, type = "L", data = RBI) # make new variable career_year based on the# start year for each player (ID) base2 <-ddply (baseball,. ( ID), Mutate,career_year = Year-min (year) + 1) head (BASE2)
         ID year stint team LG   G  AB   r   H x2b x3b HR RBI SB CS BB So IBB HBP sh sf gidp career_year1 aaronha01 1954
   1  ML1 NL 122 468  6 131  2  2 6  NA   3  4  13           Aaronha01 1955     1  ML1 NL 153 602  9 189  3  1 106  5 3 7  4           aaronha01 1956     1  ML1 NL 153 609 106  2  4 37 54   6   2  5  7           aaronha01 1957     1  ML1 NL 151 615 118 198   6 44 132< C48/>1  1   0  0  3           aaronha01 1958     1  ML1 NL 153 601 109 196   4  4  1   1  0  3           aaronha01 1959     1  ML1 NL 154 629 223   7 123  8  0 Wuyi   4  0  9 +           6

The ddply and aggregate of the R language weapon

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.