【问题标题】:How to extract column index of a dataframe with the variable name?如何使用变量名提取数据框的列索引?
【发布时间】:2021-10-16 14:51:06
【问题描述】:

我想使用变量名提取数据框变量的列索引。

这里是df的例子:

>df 

  Mean  Var  Max
a  1     0.5  3
b  1.5   0.4  4
c  0.7   0.3  2.5
d  0.3   0.1  0.5

我想“扭转”这个:

> variable.names(df[2])
[1] "Var"

类似的东西:

> variable.names(df$Var)
NULL

但是得到“2”而不是“NULL”

这是我的全部问题:

my_fct ← function(data, v_cont, v_cat){  
  for (i in 1:nlevels(as.factor(v_cat))){      
      sub <- subset(data , v_cat == levels(as.factor(v_cat))[i])     
      sub_stat <- c(levels(as.factor(v_cat))[i],                    
                    mean( **sub[,COLINDEX(v_cat)**] , na.rm = TRUE)     
      mat_stat <- rbind(mat_stat, sub_stat)

sub[,COLINDEX(v_cat) 是需要解决的。如何在新创建的新矩阵中选择初始变量?

注意:v_cat 和 v_cont 的形式如下: df$variable1 , df$variable2

感谢您的帮助

【问题讨论】:

    标签: r dataframe indexing


    【解决方案1】:

    情况并不完全清楚。但是根据提供的函数,它可以通过传递列名和子集来重写[[,而不是传递df$variable1或df$variable2

    my_fct <- function(data, v_cont, v_cat){  
      mat_stat <- NULL
      for (i in 1:nlevels(as.factor(data[[v_cat]]))){      
          sub <- subset(data , data[[v_cat]] == 
                  levels(as.factor(data[[v_cat]]))[i])     
          sub_stat <- c(levels(as.factor(data[[v_cat]]))[i],                    
                        mean(sub[,v_cat] , na.rm = TRUE)  
           mat_stat <- rbind(mat_stat, sub_stat)
                        
        }
        return(mat_stat)
    }
    

    -测试

    my_fct(df, "variable1", "variable2")
    

    如果输入是df$variable1、df$variable2,则使用 OP 的原始函数,一个选项是使用 deparse(subsitute 捕获参数,使用 sub 提取列名并将其用作列名

    my_fct <- function(data, v_cont, v_cat){ 
      nm1 <- sub(".*\\$", "", deparse(substitute(v_cat)))
      mat_stat <- NULL
      for (i in 1:nlevels(as.factor(v_cat))){      
          sub <- subset(data , v_cat == levels(as.factor(v_cat))[i])     
          sub_stat <- c(levels(as.factor(v_cat))[i],                    
                        mean(sub[, nm1] , na.rm = TRUE)     
          mat_stat <- rbind(mat_stat, sub_stat)
          }
          
          return(mat_stat)
          
    }
    

    -测试

    my_fct(df, df$variable1, df$variable2)
    

    【讨论】:

      【解决方案2】:

      类似于 LMc(+1) 解决方案 -> 我们可以使用 grep:

      df <- structure(list(Mean = c(1, 1.5, 0.7, 0.3), Var = c(0.5, 0.4, 
      0.3, 0.1), Max = c(3, 4, 2.5, 0.5)), class = "data.frame", row.names = c("a", 
      "b", "c", "d"))
      
      grep("Var", colnames(df))
      

      输出:

      [1] 2
      

      【讨论】:

      • 嘿,非常感谢,但实际上我不能使用“Var”,而只能使用 df$Var。还有其他想法吗?
      • df 应该加载到您的环境中。然后就可以轻松使用上面的代码了!查看我的更新!
      • 这是另一种情况。你应该更新你的问题!
      • 而不是df$variable1和df$variable2,你可以传递"variable1","variable2"然后用df[[v_cont]]子集数据df[[v_cat]]这样你可以得到列名这样就不必使用deparse(substitute( 等?
      • @TarJae 没关系。找到 OP 要求的索引是您的答案
      【解决方案3】:

      使用match:

      match("Var", colnames(df))
      

      【讨论】:

      • 嘿,非常感谢,但实际上我不能使用“Var”,而只能使用 df$Var。还有其他想法吗?
      【解决方案4】:

      这应该使用which来完成

      df <- data.frame(Mean=c(1,1.5,0.7,0.3),Var=c(0.5,0.4,0.3,0.1),Max=c(3,4,2.5,0.5))
      df
      
        Mean Var Max
      1  1.0 0.5 3.0
      2  1.5 0.4 4.0
      3  0.7 0.3 2.5
      4  0.3 0.1 0.5
      
      
      which(colnames(df)=="Var")
      

      输出:

      [1] 2
      

      【讨论】:

      • 嘿,非常感谢,但实际上我不能使用“Var”,而只能使用 df$Var。还有其他想法吗?
      猜你喜欢
      • 2017-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-05
      • 2020-06-05
      • 2016-10-20
      • 2022-01-20
      • 1970-01-01
      相关资源
      最近更新 更多