设为首页 加入收藏

TOP

R语言利器之ddply和aggregate(一)
2017-10-09 14:23:54 】 浏览:10337
Tags:语言 利器 ddply aggregate

  ddply和aggregate是两个用来整合数据的功能强大的函数。

  aggregate(x, ...)

  关于aggregate()函数的使用在《R语言实战》中P105有简单描述,这里重新说一下。此函数主要有一下几种用法:
  

    ## Default S3 method:
  aggregate(x, ...)

  ## S3 method for class 'data.frame'
  aggregate(x, by, FUN, ..., simplify = TRUE, drop = TRUE)

  ## S3 method for class 'formula'
  aggregate(formula, data, FUN, ...,subset, na.action = na.omit)

  ## S3 method for class 'ts'
  aggregate(x, nfrequency = 1, FUN = sum, ndeltat = 1,ts.eps = getOption("ts.eps"), ...) 

 

 


例:    

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     carb
1       4       3 21.500   4 120.1000  97.0000 3.700000 2.465000 20.0100 1.0 0.00    3 1.000000
2       6       3 19.750   6 241.5000 107.5000 2.920000 3.337500 19.8300 1.0 0.00    3 1.000000
3       8       3 15.050   8 357.6167 194.1667 3.120833 4.104083 17.1425 0.0 0.00    3 3.083333
4       4       4 26.925   4 102.6250  76.0000 4.110000 2.378125 19.6125 1.0 0.75    4 1.500000
5       6       4 19.750   6 163.8000 116.5000 3.910000 3.093750 17.6700 0.5 0.50    4 4.000000
6       4       5 28.200   4 107.7000 102.0000 4.100000 1.826500 16.8000 0.5 1.00    5 2.000000
7       6       5 19.700   6 145.0000 175.0000 3.620000 2.770000 15.5000 0.0 1.00    5 6.000000
8       8       5 15.400   8 326.0000 299.5000 3.880000 3.370000 14.5500 0.0 1.00    5 6.000000

   得到数据框aggdata,其中的Group.1和Group.2的列名可以指定,只需第二行写成:

aggdata <-aggregate(mtcars, by=list(Group.cyl=cyl, Group.gears=gear),FUN=mean, na.rm=TRUE)

 即可。

  注意:在使用aggregate()函数的时候, by中的变量必须在一个列表中(即使只有一个变量) 。 指定的函数FUN可为任意的内建或自编函数 。

  其他的一些例子:  

## Compute the averages for the variables in 'state.x77', grouped
## according to the region (Northeast, South, North Central, West) that
## each state belongs to.
aggregate(state.x77, list(Region = state.region), mean)
## Compute the averages according to region and the occurrence of more
## than 130 days of frost.
aggregate(state.x77,
          list(Region = state.region,Cold = state.x77[,"Frost"] > 130),
          mean)
## (Note that no state in 'South' is THAT cold.)
## example with character variables and NAs
testDF <- data.frame(v1 = c(1,3,5,7,8,3,5,NA,4,5,7,9),
                     v2 = 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, 12)
by2 <- c("wet", "dry", 99, 95, NA, "damp", 95, 99, "red", 99, NA, NA)
aggregate(x = testDF, by = list(by1, by2), FUN = "mean")
# and if you want to treat NAs as a group
fby1 <- 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(cbind(ncases, ncontrols) ~ alcgp + tobgp, data = esoph, sum)
## Dot notation:
aggregate(. ~ Species, data = iris, mean)
aggregate(len ~ ., data = ToothGrowth, mean)
## Often followed by xtabs():
ag <- aggregate(len ~ ., data = ToothGrowth, mean)
xtabs(len ~ ., data = ag)
## Compute the average annual approval ratings for American presidents.
aggregate(presidents, nfrequency = 1, FUN = mean)
## Give the summer less weight.
aggregate(presidents, nfrequency = 1,
          FUN = weighted.mean, w = c(1, 1, 0.5, 1))

   ddply

  下面是ddply函数的一般用

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇R语言时间序列数据应用xts 下一篇gplots heatmap.2和ggplot2 geom_..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目