[英]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.我正在写一个 function 如果列中的值为 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:
为简化起见,function 可以是:
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我认为您不需要 function ,您可以
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]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.