Slight improvement on my earlier attempt.
##================================================
library(data.table)

DT <- fread("raw.data.csv")

mydate <- DT[ , date]
mydate <- rep(mydate, 2)

id <- rep(c("ABC", "DEF"), each = 10)

DT1 <- DT[, date := NULL]

num_chunks <- ncol(DT1) / 4
cols_per_chunk <- 4

DT2 <-  split_df <- split.default(DT1, rep(1:num_chunks, each =
cols_per_chunk))

names(DT2[[1]]) <- names(DT2[[2]]) <- c("price1", "vol1", "price2","vol2")

DT2  <- rbindlist(DT2)

DT2 <- cbind(DT2, mydate, id)

##=================================================

On Wed, 18 Jun 2025 at 16:00, Ivan Krylov via R-help <r-help@r-project.org>
wrote:

> В Wed, 18 Jun 2025 10:27:37 +0000
> Naresh Gurbuxani <naresh_gurbux...@hotmail.com> пишет:
>
> > mydt <- data.table(
> > date = seq(as.IDate("2025-06-01"), by = 1, length.out = 10),
> > ABC1_price = runif(10, 100, 120), ABC1_volume = runif(10, 200, 300),
> > ABC2_price = runif(10, 100, 120), ABC2_volume = runif(10, 200, 300),
> > DEF1_price = runif(10, 100, 120), DEF1_volume = runif(10, 200, 300),
> > DEF2_price = runif(10, 100, 120), DEF2_volume = runif(10, 200, 300))
> >
> > mydt[
> > , let(ABC_price = fifelse(ABC1_volume < ABC2_volume, ABC2_price,
> > ABC1_price),
> > DEF_price = fifelse(DEF1_volume < DEF2_volume, DEF2_price,
> > DEF1_price)) ]
>
> Thank you for the reproducible example! How about the following?
>
> for (var in c("ABC", "DEF")) mydt[,
>  price := fifelse(volume1 < volume2, price2, price1),
>  env = list(
>   price = paste0(var, '_price'),
>   price1 = paste0(var, '1_price'),
>   price2 = paste0(var, '2_price'),
>   volume1 = paste0(var, '1_volume'),
>   volume2 = paste0(var, '2_volume')
>  )
> ]
>
> --
> Best regards,
> Ivan
>
> ______________________________________________
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> https://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>


-- 
John Kane
Kingston ON Canada

        [[alternative HTML version deleted]]

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

Reply via email to