Is there an equivalent R function to Stata 'order' command?

后端 未结 6 1233
旧时难觅i
旧时难觅i 2021-01-13 11:09

\'order\' in R seems like \'sort\' in Stata. Here\'s a dataset for example (only variable names listed):

v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v

6条回答
  •  庸人自扰
    2021-01-13 11:56

    I get your problem. I now have code to offer:

    move <- function(data,variable,before) {
      m <- data[variable]
      r <- data[names(data)!=variable]
      i <- match(before,names(data))
      pre <- r[1:i-1]
      post <- r[i:length(names(r))]
      cbind(pre,m,post)
    }
    
    # Example.
    library(MASS)
    data(painters)
    str(painters)
    
    # Move 'Expression' variable before 'Drawing' variable.
    new <- move(painters,"Expression","Drawing")
    View(new)
    

提交回复
热议问题