[R] Subset and 0 replace?
murdoch.duncan at gmail.com
Thu May 21 01:30:04 CEST 2015
On 20/05/2015 7:13 PM, Vin Cheng wrote:
> I'm trying to group rows in a dataframe with SPCLORatingValue factor >16 and summing the Wgt's that correspond to this condition. There are 100 dataframes in a list.
> Some of the dataframes won't have any rows that have this condition SPCLORatingValue>16 and therefore no corresponding weight.
> My problem is that I need to have a corresponding value for each dataframe in the list - so 100 values.
> If dataframe 44 doesn't have any SPCLORatingValue>16, then I end up getting a vector that's 99 long vs. 100. putting value 45 into 44's slot and so on.
> Is there either an if/else statement or argument I can place into subset to put a 0 for the data frames that don't have SPCLORatingValue>16?
> GenEval[18,1:100] <- t(summaryBy(Wgt.sum~as.numeric(.id),data=subset(ldply(Generation,function(x) summaryBy(Wgt ~ SPCLORatingValue, data=x, FUN=c(sum))),SPCLORatingValue>16),FUN=c(sum),order=FALSE))
The summaryBy function is not in base R. There's a function with that
name in the doBy package; is that the one you're using?
You doing say how to do the grouping, and I can't read your code to
figure it out, but this code will do what you want with suitable inputs:
by(df, group, function(subset) with(subset, sum(Wgt[SPCLORatingValue >
where df is your dataframe, and group is a variable that defines the groups.
More information about the R-help