# [R] Conditional subtraction

arun smartpink111 at yahoo.com
Mon Apr 7 13:53:16 CEST 2014

```Hi,
Try:
indx <- as.vector(with(dat,tapply(seq_along(price), list(id), FUN= head,1)))
dat

#1   A instru_A 101.3800     2.0800        99.3000

#2   B instru_B   3.9306     2.5217         1.4089

#3   C instru_B   3.7488     2.5217         3.7488

#4   D instru_B  92.9624     2.5217        92.9624

#5   E instru_C   5.1500     3.0800         2.0700

#6   E instru_C  96.1908     3.0800        96.1908
A.K.

Dear R forum I have following data.frame dat = data.frame(key = c("A", "B", "C", "D", "E", "E"), id = c("instru_A", "instru_B", "instru_B", "instru_B", "instru_C", "instru_C"), price = c(101.38, 3.9306, 3.7488, 92.9624, 5.15, 96.1908), adj_factor = c(2.08, 2.5217, 2.5217, 2.5217, 3.08, 3.08)) > dat key       id     price         adj_factor
1   A instru_A 101.3800   2.0800
2   B instru_B   3.9306     2.5217
3   C instru_B   3.7488     2.5217
4   D instru_B  92.9624    2.5217
5   E instru_C   5.1500     3.0800
6   E instru_C  96.1908    3.0800 This is just a part of big database and ids can appear any no of times. # MY PROBLEM I need to subtract adj_factor from the price, however only from the first id only. In case of instru_A, there is only 1 id, so 2.08 should be subtracted from 101.38. The id "instru_B" is appearing 3 times. So in this case, adj_factor = 2.5217 should be subtracted from 3.9306 and rest should remain same. Similarly, id "instru_C" is appearing 2 times, hence the adj_factor = 3.08 should be subtracted from 5.15. Effectively I am looking for  > dat_new key       id     price         adj_factor   adjusted_price
1   A instru_A 101.3800   2.0800        99.3000      # price adjusted
2   B instru_B   3.9306     2.5217         1.4089      # price adjusted
3   C instru_B   3.7488     2.5217         3.7488
4   D instru_B  92.9624    2.5217        92.9624
5   E instru_C   5.1500     3.0800         2.0700      # price adjusted
6   E instru_C  96.1908    3.0800        96.1908 I tried something like adj_price = function(id, price, adj_factor)
{
id_length = length(id) if(id_length == 1) {