设为首页 加入收藏

TOP

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

ddply(.data, .variables, .fun = NULL, ..., .progress = "none",.inform = FALSE, .drop = TRUE, .parallel = FALSE, .paropts = NULL)

   例:

# Summarize a dataset by two variables
dfx <- data.frame(
  group = c(rep('A', 8), rep('B', 15), rep('C', 6)),
  sex = sample(c("M", "F"), size = 29, replace = TRUE),
  age = runif(n = 29, min = 18, max = 54)
) 
head(dfx)
 group sex      age
1     A   M 22.44750
2     A   M 52.92616
3     A   F 30.00443
4     A   M 39.56907
5     A   M 18.89180
6     A   F 50.81139
#Note the use of the '.' function to allow
# group and sex to be used without quoting
ddply(dfx, .(group, sex), summarize,mean = round(mean(age), 2),sd = round(sd(age), 2))
  group sex  mean    sd
1     A   F 40.41 14.71
2     A   M 30.35 13.17
3     B   F 34.81 12.76
4     B   M 34.04 13.36
5     C   F 35.09 13.39
6     C   M 28.53  4.57
# An example using a formula for .variables
ddply(baseball[1:100,], ~ year, nrow)
 year V1
1 1871  7
2 1872 13
3 1873 13
4 1874 15
5 1875 17
6 1876 15
7 1877 17
8 1878  3
# Applying two functions; nrow and ncol
ddply(baseball, .(lg), c("nrow", "ncol"))
 lg  nrow ncol
1       65   22
2 AA   171   22
3 AL 10007   22
4 FL    37   22
5 NL 11378   22
6 PL    32   22
7 UA     9   22
# Calculate mean runs batted in for each year
rbi <- ddply(baseball, .(year), summarise,mean_rbi = mean(rbi, na.rm = TRUE))
head(rbi)
  year mean_rbi
1 1871 22.28571
2 1872 20.53846
3 1873 30.92308
4 1874 29.00000
5 1875 31.58824
6 1876 30.13333
# Plot a line chart of the result
plot(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_year
1 aaronha01 1954     1  ML1 NL 122 468  58 131  27   6 13  69  2  2 28 39  NA   3  6  4   13           1
2 aaronha01 1955     1  ML1 NL 153 602 105 189  37   9 27 106  3  1 49 61   5   3  7  4   20           2
3 aaronha01 1956     1  ML1 NL 153 609 106 200  34  14 26  92  2  4 37 54   6   2  5  7   21           3
4 aaronha01 1957     1  ML1 NL 151 615 118 198  27   6 44 132  1  1 57 58  15   0  0  3   13           4
5 aaronha01 1958     1  ML1 NL 153 601 109 196  34   4 30  95  4  1 59 49  16   1  0  3   21           5
6 aaronha01 1959     1  ML1 NL 154 629 116 223  46   7 39 123  8  0 51 54  17   4  0  9   19           6

 

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

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目