tmp.out.sort <- tmp.out[, order(names(tmp.out))] tmp.out.sort <- tmp.out[, order(names(tmp.out)), drop=FALSE] >From your description of misbehavior with a single column, I think the drop=FALSE argument will provide the protection you need. Then you will not need the if clause. See ?`[.data.frame` for the story and examples. Rich