我想将数据表中的列从因子转换为数值,但是我正在使用“字典”(列表),所以我希望能够通过“字典”引用列变量“:
#my "dictionary"
outcomes <- list("heart attack"="Hospital 30-Day Death (Mortality) Rates from Heart Attack",
"heart failure"="Number of Patients - Hospital 30-Day Death (Mortality) Rates from Heart Failure",
"pneumonia"="Number of Patients - Hospital 30-Day Death (Mortality) Rates from Pneumonia")
#named variable
outcome <- outcomes[["heart attack"]]
#conversion function
as.numeric.factor <- function(x) {as.numeric(levels(x))[x]}
#convert my data table column from factor to numeric
DT[, outcome := as.numeric.factor(outcome)]
理想情况下, DT[, outcome := as.numeric.factor(outcome)]
是我想要做的,但我知道我不能只在那里粘贴一个命名变量outcome
,希望数据表知道我指的是具有值outcome
。对不起,我对数据表很新,我对引用列时如何处理命名变量感到有些困惑。
答案 0 :(得分:0)
使用tidyverse
方法:
library(tidyverse)
Your_df_mod <- Your_df %>%
mutate_at(.vars=outcome,.funs=funs(as.numeric(.)))
答案 1 :(得分:0)
你可以试试这个:
library(data.table)
DT <- data.table(mtcars)[, mpg := as.factor(mpg)]
class(DT$mpg)
# [1] "factor"
as.numeric.factor <- function(x) {as.numeric(levels(x))[x]}
outcome <- "mpg"
# Reference column by assigned variable
DT[, (outcome) := as.numeric.factor(get(outcome))]
class(DT$mpg)
# [1] "numeric"