Question

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.
Was it helpful?

Solution

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), ]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top