0

I'm new to R and I have losses data:

losses=c(25,250,5,17,2,195,12,8,75,5,50,1);

How to cap each member of the list at 150? Namely how to perform min(150,x) for each member of the list?

Then I want to cap all losses at 'amount of insurance' array:

aoi=c(150,250,100,125,300,200,80,250,100,350,500,120) 
thelatemail
  • 91,185
  • 12
  • 128
  • 188
fika_fika
  • 111
  • 1
  • 10

1 Answers1

2

See ?pmin, or parallel minima calculation:

pmin(150,losses)
#[1]  25 150   5  17   2 150  12   8  75   5  50   1

If you need to do this multiple times, it would be beneficial to collect your variables in a data.frame or list. E.g.:

dat <- data.frame(losses,aoi)
data.frame(Map(pmin,dat,150))

#   losses aoi
#1      25 150
#2     150 150
#3       5 100
#etc...
thelatemail
  • 91,185
  • 12
  • 128
  • 188
  • I always seem to get pmin and pmax confused. `pmin` is for setting a maximum and `pmax` is for setting a minimum. – IRTFM Dec 02 '14 at 00:56
  • @BondedDust - the OP's description of what they want to do is actually a neat definition of `pmin` - "_perform min(150,x) for each member of the list?_" - which could be transferred straight to `sapply(losses, function(x) min(150,x) )` – thelatemail Dec 02 '14 at 00:59
  • I wasn't saying this question confused me, only that I confuse myself when I'm at the keyboard when I think ... now I need to set a "max". – IRTFM Dec 02 '14 at 01:02
  • Superb, does exactly what is needed with minimum fuss and bother. – Dr Raveem Ismail Oct 01 '15 at 00:24