I have a large data.table with hundreds of columns and thousands of rows. Most of the columns hold numeric values that are ratios like X/Y or Y/Z etc.
I need to flip some of these ratios so that they are transformed from Y/Z -> Z/Y. The only indicator I have of these columns is the column name which includes the substring "x/y"or "y/z".
I can get the columns that match "y/z" using grepl but I am not sure how I can use that array of logical values for apply/lapply etc. I realize that I can extract the columns (by logical indexing or .SDcols) and transform them, but I don't want to discard/ignore the remaining columns.
Lastly, I have tried to something like this
flipcols <- grepl("Y/Z", names(sites))
sites.new <- sites[, , lapply(.SD, function(x) 1/x), .SDcols = flipcols]
but there is no difference between the sites and sites.new, the columns that should have been transformed are not transformed and the summed difference between corresponding columns is 0.
Suggestions?
EDIT: Following @akrun's I tried the := operator, but it leads to other issues as follow:
# I think this fails because flipcols is a logical vector and not a list of names or indices
> sites.new <- sites[, (flipcols) := lapply(.SD, function(x) 1/x), .SDcols = flipcols]
Error in `[.data.table`(sites, , `:=`((flipcols), lapply(.SD, function(x) 1/x)), :
LHS of := isn't column names ('character') or positions ('integer' or 'numeric')
# and this seems to fail because .SDcols seems to lock the data in read-only mode
> sites.new <- sites[, which(flipcols) := lapply(.SD, function(x) 1/x), .SDcols = flipcols]
Error in assign(ii, SDenv$.SDall[[ii]], SDenv) :
cannot change value of locked binding for '.SD'
EDIT2: Here's a minimal example, the goal is to transform the columns which match "Y/Z" pattern (second and fourth in our minimal example here), while keeping the other columns unchanged and part of the result.
> dt <- data.table(matrix(rnorm(25), 5,5))
> names(dt) <- c("X/Y_1", "Y/Z_1", "X/Y_2", "Y/Z_2", "X/Y_3")
> dt
X/Y_1 Y/Z_1 X/Y_2 Y/Z_2 X/Y_3
1: 1.5972490 -0.01763484 1.10745607 -0.1416583 -0.4632829
2: 0.6629621 -0.82719204 -1.68214956 0.6145526 -0.8169235
3: -0.7491393 -0.05290791 0.63935066 1.0665537 -1.9107424
4: -0.6804972 -0.40107880 -0.01030063 1.4566075 -0.6866042
5: 0.2505391 -0.29091850 -1.95926987 0.8733446 1.3909565