创建字符串,如A[,1],A[,2],A[,3],A[,4]
循环中和下次使用parse
and eval
函数来评估你的表达式。
set.seed(123)
A <- matrix(rep(1:25,4)[order(rnorm(100))],ncol=4)
col <- ""
for (i in 1:ncol(A)){
col <- paste(col,paste0('A[,',i,']'), sep = ",")
}
## remove first comma
col <- substr(col, 2, nchar(col))
col
[1] "A[,1],A[,2],A[,3],A[,4]"
B <- eval(parse(text = paste("A[order(",col,",decreasing=TRUE),]")))