【问题标题】:cross-tabulations on multiple data.frames and columns多个data.frames和列的交叉表
【发布时间】:2012-09-13 20:11:25
【问题描述】:

我正在计算跨多个切片的大量数据帧与单个响应变量的汇总统计信息。我目前通过将 DF 列表传递给函数来做到这一点。但是我的函数必须单独指定列(即切片)。这大大加快了我的进程;但是,我认为必须有一种更有效的方法来通过 apply() 系列函数来做到这一点。我希望这里有人可以帮助我。

这是我的代码:

table1 <- function(x) {
  dl2 <- list()
  for (i in 1:length(x)) {
    z <- x[[i]]
    t.sliceA     <- addmargins(table(list(z$sliceA, z$Growing)))
    t.sliceB     <- addmargins(table(list(z$sliceB, z$Growing)))
    t.sliceC     <- addmargins(table(list(z$sliceC, z$Growing)))
    t.sliceD     <- addmargins(table(list(z$sliceD, z$Growing)))
    ...
    t.sliceAA    <- addmargins(table(list(z$sliceAA, z$Growing)))
    table.list <- list(t.sliceA, t.sliceB, t.sliceC, ... , t.sliceAA)
    names(table.list) <- c("t.sliceA", "t.sliceB", ... , "t.sliceAA")
    dl2[[i]] <- table.list
  }
  assign("dl",dl2, envir=.GlobalEnv)
}
# run the function
dl <- c(DF1, DF2, ..., DF.n)
table1(dl)

我认为必须有一种更有效的方法来通过 lapply() 执行此操作,我只需要指定所需的列。我会替换这些行的东西

t.sliceA <- [blah]
...
t.sliceAA <- [blah]

类似:

apply(z[,c(1:4,10:12,15)],2, function(x) addmargins(table(list(x,z$Growing))))

您能提供的任何帮助都会非常有帮助。谢谢!

更新:可重现的示例 @追赶 如果这做得不好,我很抱歉。这是我第一次使用github。

https://gist.github.com/3719220

这是代码:

# load the example datasets
a.small <- dget("df1.txt")
l.small <- dget(df2.txt)

# working function that I'd like to simplify
table1 <- function(x) {
  dl2 <- list()
  for (i in 1:length(x)) {
    z <- x[[i]]
    t.tenure     <- addmargins(table(list(z$Tenure.Group, z$Growing)))
    t.optfile    <- addmargins(table(list(z$opt.file, z$Growing)))
    t.checking   <- addmargins(table(list(z$checking, z$Growing)))
    t.full      <- addmargins(table(list(z$add.full, z$Growing)))
    t.optdm      <- addmargins(table(list(z$opt.dm, z$Growing)))
    t.up         <- addmargins(table(list(z$add.up, z$Growing)))
    t.off        <- addmargins(table(list(z$offmode, z$Growing)))
    table.list <- list(t.tenure, t.optfile, t.checking, t.full, t.optdm, t.up, t.off)
    names(table.list) <- c("t.tenure", "t.optfile", "t.checking", "t.full", "t.optdm", "t.up", "t.off")
    dl2[[i]] <- table.list
  }
  assign("dl",dl2, envir=.GlobalEnv)
}
# create a DF list to send to the function
dl <- list(a.small, l.small)
table1(dl) # run the function

【问题讨论】:

  • reproducible 例子让世界转转,转转...
  • @Chase 我已经更新了示例数据。

标签: r function functional-programming


【解决方案1】:

据我所知,这可以通过几个 lapply 语句轻松完成

如果我们定义我们的函数来创建一个边距为的表格

tabulate_df <- function(DF, .what, .with) {
  table.add.margins <- function(...) addmargins(table(...))
  lapply(DF[.what], table.add.margins, DF[[.with]])
}

然后

# the columns we want to cross tabulate with `Growing`
table_names <- setdiff(names(df1), 'Growing')
df_list <- setNames(list(df1,df2), c('df1','df2'))

lapply(df_list, tabulate_df, .what = table_names, .with = 'Growing')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-09
    • 2021-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-01
    相关资源
    最近更新 更多