简体   繁体   English

如何在函数内调用动态变量名?

[英]How to call dynamic variable name inside a function?

I need to convert categorical variables into multiple dichotomous ("dummy") variables to use in a logistic regression. 我需要将分类变量转换为多个二分(“虚拟”)变量以用于逻辑回归。 Say my data frame is: 说我的数据框是:

    tdf <- data.frame(first=sample(c("A", "B", "C", "D"), 100, replace=T),
                      lobe = sample(c("RUL", "RML", "RLL", "LUL", "LLL"), 100, replace=T),
                      continuous=sample(1:100, 100),
                      smoker = sample(c("never", "less20", "more20"), 100, replace=T)
                      )

I could manually do 我可以手动做

first. <- with (tdf,  factor (first))
dummies <-  model.matrix(~ first.)
dummies <- dummies[,-1]
tdf <- cbind(tdf, dummies)

Note that it is important to call the factor "first." 请注意,将因子称为“第一”非常重要。 (or more generally, "variable.") because the dummy variables will inherit this prefix into their respective names, making it easier to identify them later ('variable1.factor2', 'variable1.factor3' etc). (或更一般地说,“变量”。)因为虚拟变量会将此前缀继承到它们各自的名称中,以便以后更容易识别它们('variable1.factor2','variable1.factor3'等)。

My question is: How can do this using a function that would programatically assign variable names: 我的问题是:如何使用以编程方式分配变量名称的函数来执行此操作:

dummify <- function(df, vectorOfColIndices) {
  cn <- colnames(df) 
  for (i in vectorOfColIndices) {
    t. <- with (tdf,  factor (df[i])) # temporary factor
    assign (cn[i], t.) # give it the proper 'Variable.' name
    dummies <-  model.matrix(~ ????) # Stuck here: how do I call this newly created structure?
    ...
  }
}

So that I can later transform a data frame like this: 这样我以后可以像这样转换数据帧:

vd <- c(1,2,4) # columns that need to be converted into dummy vars
df <- dummify(df, vd)
dummify <- function( df , col.indicies.to.add.dummies ) {

    for ( i in names( df )[ col.indicies.to.add.dummies ] ) {

        t. <- with( df , factor( df[ , i] ) )

        dummies <-  model.matrix( ~t. ) 

        colnames( dummies ) <- paste( i , levels( t. ) , sep = "." )

        dummies <- dummies[ , -1 ]

        df <- cbind( df , dummies )

    }

    df
}

Agree with Dason's comment that there are not many situations in which you should have to manually create dummies. 同意Dason的评论,即你不应该有很多情况需要手动创建假人。 And, if you do Anthony's solution is fine. 而且,如果你做安东尼的解决方案就好了。 I present this alternative just for fun :) 我提出这个替代方案只是为了好玩:)

dummify <- function(df, vectorOfColIndices) {
  for (i in vectorOfColIndices) {
    var <- paste(names(df)[i], ".", sep="")
    assign(var, df[[i]])
    df <- cbind(df, model.matrix(reformulate(var))[, -1])
  }
  return(df)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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