Update values ​​in specified columns based on external data table

I want to update values ​​in some specified columns of a data table based on external values ​​from another data table.

I know how to make this variable a variable, but I need a more efficient solution that I can automate, perhaps using lapply

The UPDATE . My dataset (equivalent mtcarsin the example below) contains other columns that I do not want to update.

For a reproducible example, first run this piece of code

# Load library
  library(data.table)

# load data
  data(mtcars)

# create another Data Table which we'll use as external reference
# this table shows the number of decimals of those variables we want to modify in mtcars
  var <- c("mpg","disp","hp")
  Decimal <- c(1,3,2)
  DT <- cbind.data.frame(var, Decimal)

# convert into data.table
  setDT(DT)
  setDT(mtcars)

My code updating column by column

mtcars[, mpg  := mpg  / 10 ^ DT[var=="mpg",  Decimal] ]
mtcars[, disp := disp / 10 ^ DT[var=="disp", Decimal] ]
mtcars[, hp   := hp   / 10 ^ DT[var=="hp",   Decimal] ]

This code works great and gives the desired result.

Desired Result

The first line is mtcarsused like this:

>     mpg disp  hp
> 1: 21.0  160 110

and now it looks like this:

>     mpg   disp   hp
> 1: 2.10  0.160 1.10

function, lapply ..

+4
2

set . , [.data.table . "mtcars" set , "j", value "mtcars" "DT $Decimal".

for(j in seq_along(mtcars)){
  set(mtcars, i=NULL, j=j, value=mtcars[[j]]/10^DT[,Decimal][j])
 }


head(mtcars)
#    mpg  disp   hp
#1: 2.10 0.160 1.10
#2: 2.10 0.160 1.10
#3: 2.28 0.108 0.93
#4: 2.14 0.258 1.10
#5: 1.87 0.360 1.75
#6: 1.81 0.225 1.05

EDIT: OP, , , , "var", "var" set, , 'var'. mtcars data.table.

 for(j in seq_along(var)){
  set(mtcars, i=NULL, j=var[j], value=mtcars[[var[j]]]/10^DT[, Decimal][j])
 }

head(mtcars)
#    mpg cyl  disp   hp drat    wt  qsec vs am gear carb
#1: 2.10   6 0.160 1.10 3.90 2.620 16.46  0  1    4    4
#2: 2.10   6 0.160 1.10 3.90 2.875 17.02  0  1    4    4
#3: 2.28   4 0.108 0.93 3.85 2.320 18.61  1  1    4    1
#4: 2.14   6 0.258 1.10 3.08 3.215 19.44  1  0    3    1
#5: 1.87   8 0.360 1.75 3.15 3.440 17.02  0  0    3    2
#6: 1.81   6 0.225 1.05 2.76 3.460 20.22  1  0    3    1
+4

, Map()

library(data.table)
## match 'DT$var' to the names of 'mtcars'
m <- chmatch(levels(DT$var)[DT$var], names(mtcars))
## update 'mtcars' with the desired operation
mtcars[, names(mtcars) := Map("/", .SD, 10 ^ DT$Decimal[m])]
## result
head(mtcars)
#     mpg  disp   hp
# 1: 2.10 0.160 1.10
# 2: 2.10 0.160 1.10
# 3: 2.28 0.108 0.93
# 4: 2.14 0.258 1.10
# 5: 1.87 0.360 1.75
# 6: 1.81 0.225 1.05

, , .mapply() Map(),

.mapply(`/`, list(.SD, 10 ^ DT$Decimal[match(DT$var, names(mtcars))]), NULL)
+4

Source: https://habr.com/ru/post/1609510/


All Articles