R使用apply on data frame返回顺序和值

时间:2015-03-12 19:13:04

标签: r apply

我的数据目前看起来像这样

x          | y          | z
2015-02-12 | 2015-02-03 | 2015-02-06
2015-01-20 | 2015-01-30 | 2015-01-15

我需要从最早的日期到最早的日期对每一行进行排序。我希望输出返回索引以及排序顺序的值。例如,我想要:

1st_index | 2nd_index | 3rd_index | 1st_value  | 2nd_value  | 3rd_value
2         | 3         | 1         | 2015-02-03 | 2015-02-06 | 2015-02-12
3         | 1         | 2         | 2015-01-15 | 2015-01-20 | 2015-01-30

我写了一个for循环,但是我有这么多行的数据,它太慢了。我想使用申请,但我真的很挣扎。

我想做类似以下的事情,但这肯定不会返回预期的输出。

myfunc <- function(x){
  a = order(x, na.last=TRUE)
  y = c(a[1],a[2],a[3],x[a[1]],x[a[2]],x[a[3]])
}

test <- apply(df, 1, function(x) myfunc(x))

提前感谢您提供任何帮助!

1 个答案:

答案 0 :(得分:1)

这是一些数据

orig = as.data.frame(split(Sys.Date() + runif(12, 100, 200), 1:3))

将数据放入'long'形式(do.call(c, unname(orig))保留orig类,可能是类似Date的类之一,否则order()将不起作用;重要的是保持数据类的整体, apply()方法没有。)

df = data.frame(row=as.vector(row(orig)), col=as.vector(col(orig)), 
                value=do.call(c, unname(orig)))

找出基于行和值的订单

o = order(df$row, df$value, na.last=TRUE)
df = df[o, , drop=FALSE]

并将结果转换为所需的输出

orig[] = split(df$value, seq_along(orig))  # original class / names
cbind(matrix(df$col, ncol=ncol(orig), byrow=TRUE), orig)

for循环实现可能是

## pre-allocate
result = cbind(matrix(0L, nrow(orig), ncol(orig)), orig)
## fill
cidx = seq_len(ncol(orig))
for (i in seq_len(nrow(result))) {
    o = order(orig[i,], na.last=TRUE)
    result[i, cidx] = o
    result[i, -cidx] = orig[i, o]
}

for循环实现可能效率不高,因为更新data.frame的行非常慢;试图提高效率将很快导致上面的“长数据框架”解决方案。到目前为止提供的各种解决方案是

f0 = function(x) {
    as.data.frame(t(apply(x, 1, function(x) {
        o = order(x, na.last=TRUE)
        c(o, x[o])
    })))
}

f1 = function(x) {
    df = data.frame(row=as.vector(row(x)), col=as.vector(col(x)), 
                   value=do.call(c, unname(x)))
    o = order(df$row, df$value, na.last=TRUE)
    df = df[o, , drop=FALSE]
    x[] = split(df$value, seq_along(x))  # original class / names
    cbind(matrix(df$col, ncol=ncol(x), byrow=TRUE), x)
}

我们知道解决方案差异

identical(f0(orig), f1(orig))
## [1] FALSE

以下是一些时间

library(microbenchmark)
microbenchmark(f0(orig), f1(orig), times=5)
## Unit: milliseconds
##     expr       min       lq     mean    median        uq       max neval
## f0(orig) 42.011069 42.12418 42.66665 42.554372 43.034768 43.933247    10
## f1(orig)  2.555936  2.59881  2.70855  2.660635  2.803732  3.017764    10

f1()似乎更接近正确和更快;也许它有点神秘,并且需要一些注意确保保留日期类。