Hello,
Try the following.
fun <- function(x){
x[["adjusted_price"]] <- x[["price"]]
x[["adjusted_price"]][1] <- x[["price"]][1] - x[["adj_factor"]][1]
x
}
res <- do.call(rbind, lapply(split(dat, dat$id), fun))
rownames(res) <- NULL
res
Hope this helps,
Rui Barradas
Hi,
Try:
indx <- as.vector(with(dat,tapply(seq_along(price), list(id), FUN= head,1)))
dat$adjusted_price <- dat$price
dat$adjusted_price[indx] <- with(dat, price[indx]-adj_factor[indx])
dat
# key idprice adj_factor adjusted_price
#1 A instru_A 101.3800 2.080099.3000
#2
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))
3 matches
Mail list logo