Skip to content
Prev 351081 / 398502 Next

Subset and 0 replace?

Can you show a small self-contained example of you data and expected
results?
I tried to make one and your expression returned a single number in a 1 by
1 matrix.

library(doBy)
Generation<-list(
   data.frame(Wgt=c(1,2,4), SPCLORatingValue=c(10,11,12)),
   data.frame(Wgt=c(8,16), SPCLORatingValue=c(15,17)),
   data.frame(Wgt=c(32,64), SPCLORatingValue=c(19,20)))
 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))
#              1
#Wgt.sum.sum 112
str(.Last.value)
# num [1, 1] 112
# - attr(*, "dimnames")=List of 2
#  ..$ : chr "Wgt.sum.sum"
#  ..$ : chr "1"

Two ways of dealing with the problem you verbally described are
(a) determine which elements of the input you can process (e.g., which
have some values>16) and use subscripting on both the left and right
side of the assignment operator to put the results in the right place.
E.g.,
    x <- c(-1, 1, 2)
    ok <- x>0
    x[ok] <- log(x[ok])
(b) make your function handle any case so you don't have to do any
subsetting on either side.  In your case it may be easy since
sum(zeroLongNumericVector) is 0. In other cases you may want to use ifelse,
as in
   x <- c(-1, 1, 2)
   x <- ifelse(x>0, log(x), x)



Bill Dunlap
TIBCO Software
wdunlap tibco.com
On Wed, May 20, 2015 at 4:13 PM, Vin Cheng <newrnewbie at hotmail.com> wrote: