0

I'm a beginner in R and i'm working on a automation,i have a list of variables in a separate file based on which the values needs to be aggregated in the master dataset.The Master datastructure is attached Master Dataset and the referal dataset contains the vars to be aggregated Referal dataset Of the 6 variables i need to aggregate the Variables D,E,F by Sum(C)(as per the referal dataset).

The below code does my requirement manually,

X<-aggregate(C,by=list(D,E,F),FUN=sum)

But i need a code which does the same funtionality automatically.I tried making loops but the problem i face is that both datasets dont have same data.frame size. Can someone help me on this ?

Frank
  • 66,179
  • 8
  • 96
  • 180
  • 3
    Better to include your example data rather than a picture of it. Guidance over here: http://stackoverflow.com/a/28481250/1191259 – Frank Oct 12 '15 at 15:00

1 Answers1

0

So, it seems like you want to do a few things: 1) read in the master/referent datasets 2) subset the master according to the values in the referent 3) compute column sums on the master?

also, is there a specific reason you want to use aggregate()? there are probably lots of ways to do this. In any case, here is what i would do:

# assuming master is a dataframe or matrix, referent is a vector
# just simulating them here because not clear how you are reading them in
master = matrix(rnorm(36),6)
colnames(master) = c('A','B','C','D','E','F')
referent = c('D','E','F')
colSums(master[,referent])

so is that doing what you want to do? I like colSums because it's a handy built-in. I am not an R superstar though so it is possible that other ways are better for some reason.

Maximilian Press
  • 300
  • 4
  • 12
  • Subsetting the values is where i face the problem , Since my dataset is very large and i could not feed all values from refferal datasets manually, The aggregate() function provides me the sum of values for each attributes based on Variable C. And i need to make a generalized code (i,e) I should not select variables by their names,they should fall in loop once i mention the dataset name.:) – Raja Xavier Oct 13 '15 at 06:53