Re: [R] From strings to column names

2025-06-18 Thread Ivan Krylov via R-help
В Wed, 18 Jun 2025 10:27:37 + Naresh Gurbuxani пишет: > 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

Re: [R] From strings to column names

2025-06-18 Thread John Kane
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 d

Re: [R] From strings to column names

2025-06-18 Thread Bert Gunter
I should note that if I have correctly understood your query, paste0 is vectorized, so you can create vectors of names e.g. by: nmvec <- paste("a", 1:5) and then reference these names as needed. I should also add that you may do better using ?with and/or ?within to access the columns in a data f

Re: [R] From strings to column names

2025-06-18 Thread Bert Gunter
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]

[R] From strings to column names

2025-06-18 Thread Naresh Gurbuxani
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),