My data.table has many columns in groups of four. On each group, I want to perform the same set of calculations. 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)) ] # Now use mydt[, list(ABC_price, DEF_price)] for subsequent calculations With many columns, above method is error-prone and tedious. Is it possible to do something like: myvars <- c("ABC", "DEF") for (avar in myvars) { # code for above calculations } Thanks, Naresh
If I understand correctly, see ?"[" : data tables inherit from data frames, and data frame columns can be indexed by their names as character strings. The following should give you the idea: d <- data.frame(a1 = 1:3, a2 = 4:6) x <- vector("numeric",2) for(i in 1:2){ nm <- paste0("a",i) x[i] <- sum(d[, nm]) } x Please note: This is **absoutely** awful R code just meant to illustrate the sort of thing that you want to do, not as a strict model to be followed. Also, as I don't use data tables, there may be better ways to do this using data table's enhanced functionality directly. Cheers, Bert On Wed, Jun 18, 2025 at 3:27?AM Naresh Gurbuxani < naresh_gurbuxani at hotmail.com> wrote:> My data.table has many columns in groups of four. On each group, I want > to perform the same set of calculations. > > 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)) > ] > > # Now use mydt[, list(ABC_price, DEF_price)] for subsequent calculations > > With many columns, above method is error-prone and tedious. Is it > possible to do something like: > > myvars <- c("ABC", "DEF") > for (avar in myvars) { > # code for above calculations > } > > Thanks, > Naresh > > ______________________________________________ > R-help at 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. >[[alternative HTML version deleted]]
Here is a "brute strength and stupidity" approach that may help. Note I renamed your data.table to DT for my own convenience. ##===========================================## # Load packages ------------------------------------------------------------ suppressMessages(library(data.table)) # Load dada. -------------------------------------------------------------- DT <- fread("raw.data.csv"); DT # Subset data and rbind into one new data.table. ------------------------- mynames <- c("date", "price1", "vol1", "price2","vol2", "id") ABC <- DT[, c(1, 2:5)][, id := "ABC"] ; names(ABC) <- mynames DEF <- DT[, c(1, 6:9)][, id := "DEF"] ; names(DEF) <- mynames DT1 <- rbind(ABC, DEF) # Create new variable "price". -------------------------------------------- DT1[, price := fcase(vol1 < vol2, price2, default = price1)][] # Summary statistic using "by". ------------------------------------------- DT1[, .(mysum = sum(price)), by = id] ##===========================================## On Wed, 18 Jun 2025 at 06:27, Naresh Gurbuxani <naresh_gurbuxani at hotmail.com> wrote:> My data.table has many columns in groups of four. On each group, I want > to perform the same set of calculations. > > 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)) > ] > > # Now use mydt[, list(ABC_price, DEF_price)] for subsequent calculations > > With many columns, above method is error-prone and tedious. Is it > possible to do something like: > > myvars <- c("ABC", "DEF") > for (avar in myvars) { > # code for above calculations > } > > Thanks, > Naresh > > ______________________________________________ > R-help at 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]]
? Wed, 18 Jun 2025 10:27:37 +0000 Naresh Gurbuxani <naresh_gurbuxani at 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