0

everyone. I am a beginner in R with a question I can't quite figure out. I've created multiple queries within Stack Overflow to address my question (links to results here, here, and here) but none have addressed my issue. On to the problem: I have subset data frame DAV from a larger dataset.

> str(DAV)

'data.frame':   994 obs. of  9 variables:

$ MIL.ID     : Factor w/ 18840 levels "","0000151472",..: 7041 9258 10513 5286 5759 5304 5312 5337 5337 5547 ...

$ Name       : Factor w/ 18395 levels "","  Atticus Finch",..: 1226 6754 12103 17234 2317 14034 15747 4542 4542 14819 ...

$ Center     : int  2370 2370 2370 2370 2370 2370 2370 2370 2370 2370 ...

$ Gift.Date  : Factor w/ 339 levels "","01/01/2015",..: 6 6 6 7 10 13 13 13 13 13 ...

 $ Gift.Amount: num  100 47.5 150 41 95 ...

$ Solic.     : Factor w/ 31 levels "","aa","ac","an",..: 20 31 20 29 20 8 28 8 8 8 ...

$ Tender     : Factor w/ 10 levels "","c","ca","cc",..: 3 2 3 5 2 9 3 9 9 9 ...
 $ Account    : Factor w/ 16 levels "","29101-0000",..: 4 4 4 11 2 11 2 11 2 11 ...

$ Restriction: Factor w/ 258 levels "","AAU","ACA",..: 216 59 216 1 137 1 137 1 38 1 ...

The two relevant columns for my issue are MIL.ID, which contains a unique ID for a donor, and Gift.Amount, which contains a dollar amount for a single gift the donor gave. A single MIL.ID is often associated with multiple Gift.Amount entries, meaning that donor has given on multiple different occasions for various amounts. Here is what I want to do:

  1. I want to separate out the above mentioned columns from the rest of the data frame;
  2. I want to sum(Gift.Amount) but only do so for each donor, i.e. I want to create a sum of all gifts for MIL.ID 1234 in the above data.frame; and
  3. I want to rank all the MIL.IDs based on the sum Gift.Amount entries associated with their ID.

I apologize for how basic this is, and if it is redundant to a question already asked, but I couldn't find anything.

Edit to address comment:

shot of table

    > print(ranking)

Desired output

I am struggling to get the formatting correct here so I included screen shots

Community
  • 1
  • 1
Lee
  • 224
  • 1
  • 2
  • 9
  • share your data in a format that others can read in, and show your expected output – mtoto Feb 15 '16 at 17:27
  • plyr would be a great package to help you here, and it has lots of helpful info if you do a quick web search. – shirewoman2 Feb 15 '16 at 17:32
  • Please don't post pictures of data. Provide a reproducible example by using `dput`. Same with your output. Also read this http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example. – Sotos Feb 15 '16 at 19:28
  • I apologize. Due to HIPAA considerations, I can't dput the files as it would give people's real names. I will review the link and do better to get my question answered. Thanks for the heads up. – Lee Feb 15 '16 at 19:49

1 Answers1

1

This should do it:

df <- DAV[, c("MIL.ID", "Gift.Amount")] #extract columns
df <- aggregate(Gift.Amount ~ MIL.ID, df, sum) #sum amounts with same ID
df <- df[ order(df$Gift.Amount,decreasing = TRUE), ] #sort Decreasing
Sotos
  • 51,121
  • 6
  • 32
  • 66