在循环中应用熔体

时间:2014-11-20 09:38:33

标签: r reshape

我有一个数据框列表,我想融化,然后添加一个带有因子的列(此处设置)。 然后最终结合起来。但是,我在尝试使用熔化物和lapply时遇到错误(我会用do.call,rbind列表)但是我仍然坚持申请融化。

任何帮助都表示赞赏,也许我没有通过'设置'正确。

Melt<-function(df,set){
df_mlt<-melt(df)
colnames(df_mlt)<-c("pt","col","Int")
df_mlt<-transform(df_mlt,set=rep(set,times=dim(df_mlt)[1]))
}
files_mlted<-lapply(names(files_shifted),function(x) Melt(files_shifted[[x]],x))

Error: evaluation nested too deeply: infinite recursion / options(expressions=)?
Error during wrapup: evaluation nested too deeply: infinite recursion / options(expressions=)?

> str(files_shifted)
List of 3
 $ EcEcDelta      : num [1:45, 1:12] NA NA NA 4184 4198 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : NULL
  .. ..$ : chr [1:12] "line 1" "line 2" "line 3" "line 4" ...
 $ MesecMesecDelta: num [1:46, 1:7] 4708 4679 4674 4747 4717 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : NULL
  .. ..$ : chr [1:7] "line 1" "line 2" "line 3" "line 4" ...
 $ MesecMesDelta  : num [1:51, 1:12] NA NA 4408 4472 4581 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : NULL
  .. ..$ : chr [1:12] "line 1" "line 2" "line 3" "line 4" ...



structure(list(EcEcDelta = structure(c(NA, NA, NA, 4183.9897, 
4197.9263, 4169.5312, 4164.3252, 4413.3159, 4396.481, 4155.8994, 
4110.7871, 4296.3462, 4497.458, 4516.624, 4597.8345, 4680.0024, 
4800.0811, 4992.167, 5375.4189, 5933.1914, 6589.8003, 7691.2852, 
8650.3418, 8643.0215, 7475.228, 6215.3271, 5240.209, 4646.1699, 
4358.4946, 4203.7007, 4200.9126, 4292.7974, 4290.2119, 4035.3877, 
3824.6089, 3694.5603, 3794.0649, 3835.3845, 3743.8027, 3839.2417, 
3944.7722, 3975.55, 3994.0811, 4193.9482, 4220.0361, NA, NA, 
, NA), .Dim = c(45L, 12L), .Dimnames = list(
NULL, c("line 1", "line 2", "line 3", "line 4", "line 5", 
"line 6", "line 7", "line 8", "line 9", "line 10", "line 11", 
"line 12"))), MesecMesecDelta = structure(c(4708.2158, 4678.9521, 
4674.4448, 4747.0122, 4717.1528, 4855.1533, 4823.2969, 4729.3257, 
4474.9043, 4478.2461, 4659.1538, 4626.4385, 4698.3784, 4795.8794, 
4914.1973, 5186.6772, 5517.021, 5717.6484, 6304.8179, 7310.9424, 
8474.2725, 9362.792, 9325.4678, 8488.7021, 7503.6167, 7087.0112,  
4211.4683, 4333.1514, 4346.751, 4350.1226, 4115.3848, 3880.6357, 
3890.1731, 3747.5061, NA, NA, NA, NA, NA, NA, NA, NA), .Dim = c(46L, 
7L), .Dimnames = list(NULL, c("line 1", "line 2", "line 3", "line 4", 
"line 5", "line 6", "line 7"))), MesecMesDelta = structure(c(NA, 
NA, 4407.7998, 4471.9248, 4581.3101, 4618.3403, 4529.812, 4604.5894, 
4775.9009, 4824.9214, 4684.2368, 4594.7852, 4447.998, 4347.6182, 
4370.4023, 4211.9419, 4262.7798, 4381.9023, 4452.2407, 4653.1538, 
4997.3403, 5659.2056, 6694.8037, 7665.9014, 7676.9712, 7077.6855, 
3600.1545, 3739.146, 3968.1042, 4155.644, 4273.3623, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 3955.3906, 3826.8286, 
3217.3462, 3034.8352, 2965.7563, 3042.1465, 3121.8101, 3150.625, 
3139.7209, 3178.7612, 3373.1699, 3699.7705, 3920.2891, 3914.2915, 
NA, NA, NA, NA, NA, NA, NA, NA), .Dim = c(51L, 12L), .Dimnames = list(
NULL, c("line 1", "line 2", "line 3", "line 4", "line 5", 
"line 6", "line 7", "line 8", "line 9", "line 10", "line 11", 
"line 12")))), .Names = c("EcEcDelta", "MesecMesecDelta", 
"MesecMesDelta"))

2 个答案:

答案 0 :(得分:1)

尝试

library(reshape2)
set <- 'A'
res <- transform(do.call(`rbind`,lapply(lst, melt)), set=set)
row.names(res) <- NULL

假设您在set中有多个元素(长度等于lst的长度),并且想要在每个列表元素中为相应的set元素创建一个新列

set <- LETTERS[1:3]
res1 <- do.call(`rbind`,Map(cbind, lapply(lst, melt), set=set))
row.names(res1) <- NULL
head(res1,3)
#   Var1   Var2 value set
#1    1 line 1    NA   A
#2    2 line 1    NA   A
#3    3 line 1    NA   A

数据

lst <- structure(list(EcEcDelta = structure(c(NA, NA, NA, 4183.9897, 
4197.9263, 4169.5312, 4164.3252, 4413.3159, 4396.481, 4155.8994, 
4110.7871, 4296.3462, 4497.458, 4516.624, 4597.8345, 4680.0024, 
4800.0811, 4992.167, 5375.4189, 5933.1914, 6589.8003, 7691.2852, 
8650.3418, 8643.0215, 7475.228, 6215.3271, 5240.209, 4646.1699, 
4358.4946, 4203.7007, 4200.9126, 4292.7974, 4290.2119, 4035.3877, 
3824.6089, 3694.5603, 3794.0649, 3835.3845, 3743.8027, 3839.2417, 
3944.7722, 3975.55, 3994.0811, 4193.9482, 4220.0361, NA, NA,NA),
.Dim = c(4L, 12L), .Dimnames = list(NULL,
c("line 1", "line 2", "line 3", "line 4", "line 5", 
"line 6", "line 7", "line 8", "line 9", "line 10", "line 11", 
"line 12"))), MesecMesecDelta = structure(c(4708.2158, 4678.9521, 
4674.4448, 4747.0122, 4717.1528, 4855.1533, 4823.2969, 4729.3257, 
4474.9043, 4478.2461, 4659.1538, 4626.4385, 4698.3784, 4795.8794, 
4914.1973, 5186.6772, 5517.021, 5717.6484, 6304.8179, 7310.9424, 
8474.2725, 9362.792, 9325.4678, 8488.7021, 7503.6167, 7087.0112,  
4211.4683, 4333.1514, 4346.751, 4350.1226, 4115.3848, 3880.6357, 
3890.1731, 3747.5061, NA, NA, NA, NA, NA, NA, NA, NA), .Dim = c(6L, 
7L), .Dimnames = list(NULL, c("line 1", "line 2", "line 3", "line 4", 
"line 5", "line 6", "line 7"))), MesecMesDelta = structure(c(NA, 
NA, 4407.7998, 4471.9248, 4581.3101, 4618.3403, 4529.812, 4604.5894, 
4775.9009, 4824.9214, 4684.2368, 4594.7852, 4447.998, 4347.6182, 
4370.4023, 4211.9419, 4262.7798, 4381.9023, 4452.2407, 4653.1538, 
4997.3403, 5659.2056, 6694.8037, 7665.9014, 7676.9712, 7077.6855, 
3600.1545, 3739.146, 3968.1042, 4155.644, 4273.3623, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 3955.3906, 3826.8286, 
3217.3462, 3034.8352, 2965.7563, 3042.1465, 3121.8101, 3150.625, 
3139.7209, 3178.7612, 3373.1699, 3699.7705, 3920.2891, 3914.2915, 
NA, NA, NA), .Dim = c(5L, 12L), .Dimnames = list(
NULL, c("line 1", "line 2", "line 3", "line 4", "line 5", 
"line 6", "line 7", "line 8", "line 9", "line 10", "line 11", 
"line 12")))), .Names = c("EcEcDelta", "MesecMesecDelta", 
"MesecMesDelta")) 

答案 1 :(得分:1)

对不起,迟到的答案,但我想我会为类似情况的人添加它。我看到了一些类似的未解答的问题。

我以前称我的功能'融化'而不是熔化。并且这已写入函数'melt from reshape2',因此它被卡在了一个infinte循环中。

因此请注意命名自己的函数,与基本R函数同名。