简体   繁体   中英

Data table in r, assign value to a specific column not using it's name

I am writing a function which will impute zero if a value in a column is NA. The tables I will need to impute will be in a format of:

tab = data.table(V1 = 1, var = NA, perc = NA) 

Tables will have different column names but the one to impute will always be the second one. To simplify, the function could be:

impute = function(DT, variable) {
DT[is.na(get(variable)), variable := 0]
}

That second 'variable' needs to be wrapped in something to work I assume. I would like to point it to the

variable = colnames(tab)[2]

Can anyone help please

You can wrap variable in () :

library(data.table)

impute = function(DT, variable) {
    DT[is.na(get(variable)), (variable) := 0]
}

variable = colnames(tab)[2]
impute(tab, variable)

tab
#   V1 var perc
#1:  1   0   NA 

I don't think you need a function for it, you can do

cols <- colnames(DT)[2]
DT[, (cols) := lapply(.SD, function(z) replace(z, is.na(z), 0)), .SDcols = cols]

though if you want one, you could do

na0 <- function(x, default = 0) replace(x, is.na(x), default)
DT[, (cols) := lapply(.SD, na0), .SDcols = cols]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM