我有以下代码,
z <- data.frame(a=sample.int(10),b=sample.int(10),c=sample.int(10))
letter <- c("a","c","b") # this will be used as the argument to a function
vec <- unlist(lapply(1:length(letter),
function(x) cat(paste("z[[letter[",x,"]]],",sep=""))))
vec[length(vec)] <- paste("z[[letter[",length(vec),"]]]",sep="")
最后:
> vec
[1] "z[[letter[1]]]," "z[[letter[2]]]," "z[[letter[3]]]"
我想使用下面的代码vec
来订购 dataframe 的行,z
z.sort <- z[with(z, order(???)),]
如何获取vec
要评估的字符向量作为 的参数order
?letter
考虑到, 用于形成vec
将是函数的参数,有没有更好的方法来做到这一点?
期望的输出是:
a b c
5 1 1 9
10 2 10 2
1 3 7 1
9 4 2 5
8 5 8 6
2 6 4 3
4 7 9 10
3 8 3 8
6 9 5 7
7 10 6 4
或作为dput
输出:
structure(list(a = 1:10, b = c(1L, 10L, 7L, 2L, 8L, 4L, 9L, 3L, 5L, 6L), c = c(9L, 2L, 1L, 5L, 6L, 3L, 10L, 8L, 7L, 4L)), .Names = c("a", "b", "c"), row.names = c(5L, 10L, 1L, 9L, 8L, 2L, 4L, 3L, 6L, 7L), class = "data.frame")