首页 > 解决方案 > Converting a specifc column within a function, where the column is specified as function input

问题描述

I am trying to convert a numeric column to a factor column inside a function. The # dataset$variable_name<- as.factor(dataset$variable_name) works. But obviously I do not want to have the actual variable name in the function.

library(data.table)
DT <- structure(list(variable_name= c(4, 1, 2, 4, 4, 1, 4, 4, 4, 
4)), row.names = c(NA, -10L), class = c("data.table", "data.frame"
))

fun <- function(dataset, factorvar) {

  # Option 1 works, but is not preferred
  # dataset$variable_name<- as.factor(dataset$variable_name)

  # Option 2 would be better but does not work
  # dataset[,1]<- as.factor(dataset[,1])

  # Option 3 would be best but does not work either
  dataset[, ..factorvar]<- as.factor(dataset[, ..factorvar])
}

DT <- fun(DT, "variable_name")

标签: rfunction

解决方案


您可以使用 :

library(data.table)

fun <- function(dataset, factorvar) {
  
  dataset[, (factorvar) := factor(get(factorvar))]
  dataset
}

fun(DT, "variable_name")
str(DT)

#Classes ‘data.table’ and 'data.frame': 10 obs. of  1 variable:
# $ variable_name: Factor w/ 3 levels "1","2","4": 3 1 2 3 3 1 3 3 3 3

推荐阅读