colwise and each should work better together #4

Closed
hadley opened this Issue Apr 28, 2010 · 1 comment

Projects

None yet

1 participant

@hadley
Owner
hadley commented Apr 28, 2010

e.g.

mF <- read.table(textConnection("
  bearID YEAR Season SEX      line54
5    1900    8      3   0  16.3923519
11   2270    5      1   0 233.7414014
12   2271    5      1   0 290.8207652
13   2271    5      2   0 244.7820844
15   2291    5      1   0   0.0000000
16   2291    5      2   0  14.5037795
17   2291    6      1   0   0.0000000
18   2293    5      2   0 144.7440752
19   2293    5      3   0   0.0000000
20   2293    6      1   0  16.0592270
21   2293    6      2   0  30.1383426
28   2298    5      1   0   0.9741067
29   2298    5      2   0   9.6641018
30   2298    6      2   0   8.6533828
31   2309    5      2   0  85.9781303
32   2325    6      1   0 110.8892153
35   2331    6      1   0  26.7335562
44   2390    7      2   0   7.1690620
45   2390    8      2   0  44.1109897
46   2390    8      3   0 503.9074898
47   2390    9      2   0   8.4393660
54   2416    7      3   0  48.6910907
58   2418    8      2   0   5.7951139"), header = TRUE)

# Test data...add multiple numeric variables
test <- data.frame(mF[, -5], x1 = rnorm(23), x2 = rnorm(23), 
                   x3 = rnorm(23))

# Want min, mean and max of each numeric column vector
# Use numcolwise composed with each:

library(plyr)
summ <- ddply(test, .(Season), numcolwise(each(min, mean, max)))
# Add vector of names to distinguish stats in each group
summ$stat <- rep(c('Min', 'Mean', 'Max'), 3)   
summ <- summ[, c(1, 5, 2:4)]   # column rearrangement
summ
#   Season stat         x1          x2          x3
#1      1  Min -1.1084957 -0.89586438 -2.07369239
#2      1 Mean -0.2590727 -0.00485722 -0.05164301
#3      1  Max  1.6924681  0.65256782  1.61998433
#4      2  Min -1.6862610 -0.94842919 -0.43556155
#5      2 Mean  0.4655741  0.37197098  0.31221804
#6      2  Max  2.0972202  2.63259653  1.19390094
#7      3  Min -1.0935199 -0.68866127 -0.04847558
#8      3 Mean -0.2049273  0.49534667  0.15200570
#9      3  Max  0.3900610  2.42638021  0.43551022
@hadley
Owner
hadley commented Mar 30, 2015

They should, but this is unlikely to ever happen

@hadley hadley closed this Mar 30, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment