按行列出data.frame并按行在每个部分上应用函数

aL3*_*3xa 2 r list apply lapply dataframe

这似乎是一个典型的plyr问题,但我有一些不同的想法.这是我想要优化的功能(跳过for循环).

# dummy data
set.seed(1985)
lst <- list(a=1:10, b=11:15, c=16:20)
m <- matrix(round(runif(200, 1, 7)), 10)
m <- as.data.frame(m)


dfsub <- function(dt, lst, fun) {
    # check whether dt is `data.frame`
    stopifnot (is.data.frame(dt))
    # check if vectors in lst are "whole" / integer
    # vector elements should be column indexes
    is.wholenumber <- function(x, tol = .Machine$double.eps^0.5)  abs(x - round(x)) < tol
    # fall if any non-integers in list
    idx <- rapply(lst, is.wholenumber)
    stopifnot(idx)
    # check for list length
    stopifnot(ncol(dt) == length(idx))
    # subset the data
    subs <- list()
    for (i in 1:length(lst)) {
            # apply function on each part, by row
            subs[[i]] <- apply(dt[ , lst[[i]]], 1, fun)
    }
    # preserve names
    names(subs) <- names(lst)
    # convert to data.frame
    subs <- as.data.frame(subs)
    # guess what =)
    return(subs)
}
Run Code Online (Sandbox Code Playgroud)

现在是一个简短的演示......实际上,我将要解释我的主要目的.我想data.frame通过在list对象中收集的向量来对a进行子集化.由于这是伴随心理研究中数据操作的函数的代码的一部分,因此您可以将其m视为人格问卷(10个科目,20个变量)的结果.列表中的向量包含定义问卷子量表(例如人格特征)的列索引.每个子量表由几个项目(列中data.frame)定义.如果我们假设每个子量表上的得分只不过是sum(或某些其他函数)行值(每个主题的调查问卷部分的结果),您可以运行:

> dfsub(m, lst, sum)
    a  b  c
1  46 20 24
2  41 24 21
3  41 13 12
4  37 14 18
5  57 18 25
6  27 18 18
7  28 17 20
8  31 18 23
9  38 14 15
10 41 14 22
Run Code Online (Sandbox Code Playgroud)

我瞥了一眼这个函数,我必须承认这个小循环并没有破坏代码...但是,如果有一个更简单/有效的方法,请告诉我!

had*_*ley 7

我采取不同的方法,并将所有内容保存为数据框,以便您可以使用merge和ddply.我认为你会发现这种方法更为通用,并且更容易检查每个步骤是否正确执行.

# Convert everything to long data frames
m$id <- 1:nrow(m)

library(reshape)
obs <- melt(m, id = "id")
obs$variable <- as.numeric(gsub("V", "", obs$variable))

varinfo <- melt(lst)
names(varinfo) <- c("variable", "scale")

# Merge and summarise
obs <- merge(obs, varinfo, by = "variable")

ddply(obs, c("id", "scale"), summarise, 
  mean = mean(value), 
  sum = sum(value))
Run Code Online (Sandbox Code Playgroud)