I have a data frame with hundreds of variables. Each variable has a numeric attribute. I would like to order the variables based on their numeric attributes. Imagine the following data frame:
df <- cbind.data.frame(v1=c(1,2,3),v2=c(1,2,3),v3=c(1,2,3))
attributes(df$v1)$myattri<-2
attributes(df$v2)$myattri<-9
attributes(df$v3)$myattri<-1
I would like to order the variables like this: v3 (smallest attribute, 1), v1 (second smallest attribute, 2) , v2 (biggest attribute, 9)
The approach by which you would order variables by name for example does not work:
df[ , order(names(df))] # orders variables by name
df[ , order(myattri(df))] # Error in myattri(df) : could not find function "myattri"
I can access the attributes on the variable level, but not on the data frame level:
attributes(df$v1)
# $myattri
# [1] 2
attributes(df)
# $names
# [1] "v1" "v2" "v3"
#
# $row.names
# [1] 1 2 3
#
# $class
# [1] "data.frame"