this is my sample data
givendata <- data.table(class = c("a", "b", "c", "d", "e", "f", "g"), type = c("x", "z", "y", "x", "x", "z", "z"), risk = c("l", "m", "n", "l", "e", "n", "n"))
And I want to create a column to the right in the data.table givendata
by matching the risk
column from the data.table givendata
to the data.table map
and bring the value
column.
this is the other data.table.
map<-data.table(fx=c('x','y','z','l','m','n','e','a','b','c','d','e','f','g'),value = runif(14))
I am using this code and it works perfectly.
givendata[map, value2 := i.value, on = .(risk = fx)]
But I need help in making this code a little dynamic.
the risk
is a user specified it can be whether class
, type
or risk
.
The way i input it is using the below code:
columnname<-read.xlsx(xlsxFile=//C:/xyz.xlsx ,namedRegion="inputcolname",colNames=FALSE)[,1]
I have tried using the below codes.
givendata[map, value2 := i.value, on = .(get(columnname)= fx)]
givendata[map, value2 := i.value, on = .(givendata[, x ,env = list(x=columnname)]= fx)]