I would like to extract the column index of a variable of a dataframe using the variable name.
here is the df for exemple:
>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
I want to "reverse" this:
> variable.names(df[2])
[1] "Var"
with something like that:
> variable.names(df$Var)
NULL
But getting "2" instead of "NULL"
here is my entire problem:
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) is what need to solve. How to select the initial variable in my new matrix freshly created?
Note: v_cat and v_cont have the following form: df$variable1 , df$variable2
thanks for helping