将dcast()函数应用于数据帧列表

时间:2016-11-15 14:44:42

标签: r plyr lapply reshape2 dcast

我有一个数据框列表,我在下面展示了一些数据框:

> str(ls.df.val.keep)

List of 87
 $ AT_VALUES_IMV_EU28_EXTRA  :'data.frame': 1554 obs. of  4 variables:
  ..$ PARTNER        : Factor w/ 3 levels "EU28_EXTRA","EU28_INTRA",..: 1 1 1 1 1 1 1 1 1 1 ...
  ..$ PRODUCT        : int [1:1554] 2207 2207 2207 2207 2207 2207 2207 2207 2207 2207 ...
  ..$ PERIOD         : Date[1:1554], format: "2010-01-01" "2010-02-01" "2010-03-01" "2010-04-01" ...
  ..$ INDICATOR_VALUE: num [1:1554] 1033828 291810 227820 104650 254214 ...
 $ AT_VALUES_IMV_EU28_INTRA  :'data.frame': 4380 obs. of  4 variables:
  ..$ PARTNER        : Factor w/ 3 levels "EU28_EXTRA","EU28_INTRA",..: 2 2 2 2 2 2 2 2 2 2 ...
  ..$ PRODUCT        : int [1:4380] 2207 2207 2207 2207 2207 2207 2207 2207 2207 2207 ...
  ..$ PERIOD         : Date[1:4380], format: "2010-01-01" "2010-02-01" "2010-03-01" "2010-04-01" ...
  ..$ INDICATOR_VALUE: num [1:4380] 1894626 2177029 1271173 994368 1746904 ...
 $ AT_VALUES_IMV_IN          :'data.frame': 6 obs. of  4 variables:
  ..$ PARTNER        : Factor w/ 3 levels "EU28_EXTRA","EU28_INTRA",..: 3 3 3 3 3 3
  ..$ PRODUCT        : int [1:6] 2208 2208 220890 220890 22089069 22089069
  ..$ PERIOD         : Date[1:6], format: "2010-09-01" "2012-07-01" "2010-09-01" "2012-07-01" ...
  ..$ INDICATOR_VALUE: num [1:6] 58 37 58 37 58 37
.
.
.
.

我想将自定义函数dcast()用于此列表的元素,这些元素是我的87个数据帧。对于每个数据帧,所需的结果应如下所示,具体取决于它具有的列数:

   22082027 22082029 22082040 22082062 22082086 22082089 22083011 22083019 22083030 22083041 22083061 22083069 22083071 22083082 22084011
1        NA       NA       NA       NA       NA       NA   400675       NA     3103       NA      186       NA       NA       NA    22297
2        NA     2594       NA       NA       NA       NA   667309     3011       NA       NA       NA       NA       NA     1916       NA
3        NA       NA       NA       NA       NA       NA       NA       NA       NA       NA       NA       NA       NA      263    93482
4        NA       NA       NA       NA       NA       NA   503155     4516       NA       NA       NA       NA       NA     5359       NA
5        NA      175       NA       NA       NA       NA   226936     4514       NA       NA       NA       NA    39389       NA       NA

我尝试了各种解决方案和代码片段,而非正在运行:

## First solution
all.cast <- list()
for (i in 1:length(ls.df.val.keep)) {
  all.cast[[i]] <- dcast(ls.df.val.keep[[i]], PERIOD ~ PRODUCT, value.var = "INDICATOR_VALUE")
}
ls.df.val.dcas <- list()
ls.df.val.dcas <- lapply(names(ls.df.val.keep), myfunc)

它产生错误:

Error: value.var (INDICATOR_VALUE) not found in input
Called from: cast(data, formula, fun.aggregate, ..., subset = subset, fill = fill, 
    drop = drop, value.var = value.var)

第二种解决方案

ls.df.val.dcas < - lapply(ls.df.val.keep, function(x) dcast(x, PERIOD ~ PRODUCT, value.var="INDICATOR_VALUE"))

错误是:

Error in dim(ordered) <- ns : 
  dims [product 1] do not match the length of object [0]

第三种解决方案

llply(ls.df.val.keep, .fun=function(x) dcast(x, PERIOD ~ PRODUCT, value.var="INDICATOR_VALUE"))

错误是:

Error in do.ply(i) : 
  task 9 failed - "dims [product 1] do not match the length of object [0]"
In addition: Warning message:
In setup_parallel() : No parallel backend registered

第四种解决方案(绝望的)

all.cast <- list()
for (i in 1:length(ls.df.val.keep)) {
  all.cast[[i]] <- dcast(ls.df.val.keep[[i]], PERIOD ~ PRODUCT, value.var = "INDICATOR_VALUE")
}

再次出现错误:

Error in dim(ordered) <- ns : 
  dims [product 1] do not match the length of object [0]

奇怪的是,如果我把每个数据帧的名称或只是列表的元素,例如ls.df.val.keep [[3]],他们工作!我还有许多其他事情要用lapply来做这个列表,但是我在第一步就被困住了! 有没有我没看到的东西?

编辑。

我正在添加@jake请求的数据帧数据样本:

> head(ls.df.val.keep[[1]])
         PARTNER    PRODUCT     PERIOD             INDICATOR_VALUE
58136 EU28_EXTRA    2207     2010-01-01         1033828
58138 EU28_EXTRA    2207     2010-02-01         291810
58140 EU28_EXTRA    220710   2010-03-01         227820
58142 EU28_EXTRA    220710   2010-04-01         104650
58144 EU28_EXTRA    22071000 2010-09-01         254214
58146 EU28_EXTRA    22071000 2010-10-01         572446

.. .. ..

1 个答案:

答案 0 :(得分:0)

拉入Pierre发现的空数据框丢失错误,您可以使用purrr来实现目标。

library(purrr)
library(dplyr)
library(reshape2)

dcast_list <- map_if(ls.df.val.keep, negate(is_empty), ~dcast(.x, PERIOD ~ PRODUCT, value.var = "INDICATOR_VALUE"))

这将返回数据帧列表,并保持空数据帧不变。如果要删除它们,请在地图之前将其过滤掉。