0

I have data which looks like the following

nums      r     a
1 3     210     0
4 6     3891    1
9 8     891     1
1 3     321     1
8 1     32      0
etc     etc     etc

I'd like to compute a few things, and was wondering if anyone can help provide code for the following

  1. The mean a value for each nums value (e.g., above, the average a for 1 3 is 0.5), sorted by the highest average a value. I'm thinking tapply would solve this, but I don't know how to handle the sort component.
  2. The mean a value for each nums value, sorted by a predetermined nums order. E.g., something like tapply(df$ac, df$nums, mean, orderBy=c("1 3", "4 6", "8 1", etc.)). You can assume I have an ordering that covers every possible nums value.
CodeGuy
  • 28,427
  • 76
  • 200
  • 317

1 Answers1

1

Using tapply:

agg <- with(data, tapply(a, nums, FUN = mean))

Then for 1, do:

sort(agg, decreasing = TRUE)

For 2, do:

agg[predetermined.nums]

You can also use aggregate:

agg <- aggregate(a ~ nums, data, FUN = mean)

Then for 1, do:

agg[order(agg$a, decreasing = TRUE), ]

For 2, do:

agg[match(predetermined.nums, agg$nums), ]
flodel
  • 87,577
  • 21
  • 185
  • 223
  • For tapply, #2 doesn't work. For aggregate, #2 works, but it outputs as a data frame, not as tapply normally outputs. – CodeGuy Nov 26 '13 at 02:06
  • 1
    Hard to tell since you did not provide a reproducible example... Can you figure it out yourself? It is simple indexing after all. – flodel Nov 26 '13 at 02:09
  • Maybe simple for you, but I am somewhat new to R. Could you reformat the output of your aggregate method to look like the output of a tapply call? – CodeGuy Nov 26 '13 at 02:17
  • 1
    Most likely you have a factor, so try `agg[as.character(predetermined.nums)]`. In the future, please provide a reproducible example and try to give more helpful feedback than "it doesn't work". – flodel Nov 26 '13 at 02:17
  • Reproducible example: data = data.frame(nums=c("1 2 3","4 3 2","2 5 2","4 3 2"),a=c(1,0,1,1)); agg=aggregate(a~nums,data,FUN=mean); agg[unique(data$nums)]; – CodeGuy Nov 26 '13 at 02:18
  • Yep, the as.character fixed it. Thanks for your help and your patience. – CodeGuy Nov 26 '13 at 02:20
  • Can you edit your question to include the reproducible example ... ? – Ben Bolker Nov 26 '13 at 02:22