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)

{
(adjusted_price = price-adj_factor)
}

if(id_length == 2)

{
(adjusted_price = c(price[1]-adj_factor[1], price[2]))
}

if(id_length > 2)

{
(adjusted_price = c(price[1]-adj_factor[1],price[2:id_length]))
}

return(adjusted_price)

}

(final_price = adj_price(dat$id, dat$price, dat$adj_factor))

> (final_price = adj_price(dat$id, dat$price, dat$adj_factor))
[1] 99.3000  3.9306  3.7488 92.9624  5.1500 96.1908


Kindly advise

Regards

Katherine
        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to