[R] Subset and 0 replace?
wdunlap at tibco.com
Thu May 21 07:12:01 CEST 2015
Can you show a small self-contained example of you data and expected
I tried to make one and your expression returned a single number in a 1 by
summaryBy(Wgt ~ SPCLORatingValue, data=x,
# 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.
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,
x <- c(-1, 1, 2)
x <- ifelse(x>0, log(x), x)
On Wed, May 20, 2015 at 4:13 PM, Vin Cheng <newrnewbie at hotmail.com> 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] <-
> summaryBy(Wgt ~ SPCLORatingValue, data=x,
> Any help or guidance would be greatly appreciated!
> Many Thanks,
> [[alternative HTML version deleted]]
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> PLEASE do read the posting guide
> and provide commented, minimal, self-contained, reproducible code.
[[alternative HTML version deleted]]
More information about the R-help