使用mdply和mutate的data.frame排列选项(从列表创建)

时间:2013-12-19 20:02:00

标签: r list dataframe plyr reshape

我正在使用mdplymutate一起使用here中的信息获取data.frame的列表:

# predictions
X1        DAY MONTH YEAR pLength pred
1.1.1.V1   1     1    1    0.00 1.00
1.1.1.V2   1     1    1    0.25 2.00
1.1.1.V3   1     1    1    1.00 1.00
2.2.1.V1   2     2    1    0.00 2.00
2.2.1.V2   2     2    1    0.50 2.50
2.2.1.V3   2     2    1    1.00 3.00
2.3.2.V1   2     3    2    0.00 2.00
2.3.2.V2   2     3    2    0.65 2.35
2.3.2.V3   2     3    2    1.00 3.00

虽然我希望结果安排如下:

# DFx
DAY MONTH YEAR pLength V1 V2 V3
1     1    1    0.00  1   NA NA
1     1    1    0.25 NA 2.00 NA
1     1    1    1.00 NA   NA  1
2     2    1    0.00  2   NA NA
2     2    1    0.50 NA 2.50 NA
2     2    1    1.00 NA   NA  3
2     3    2    0.00  2   NA NA
2     3    2    0.65 NA 2.35 NA
2     3    2    1.00 NA   NA  3

我可以在下面的代码中以不同的方式获得DFx的格式吗?我尝试过铸造predictions失败了。或者,除mutate之外还有哪些选项可以与mdply一起使用,这可能会导致我正在寻找的最终结果?

编辑:我目前的解决方案是将predictions保存为csv,在Excel中打开它,对列进行文本分割,只留下一个只包含变量名称的列(即V1,V2,V3)命名变量,将其带回r,最后dcast dcast(pred, DATE+pLength ~ variable)

df1 <- structure(list(DAY = c(1L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 
2L, 2L, 1L, 2L, 2L, 1L, 2L, 2L), MONTH = c(1L, 2L, 3L, 1L, 2L, 
3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), YEAR = c(1L, 
1L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 
1L, 2L, 1L, 1L, 2L, 1L, 1L, 2L), pLength = c(0L, 
0L, 0L, 1L, 1L, 1L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 0L, 1L, 1L, 
1L), variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 
2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("V1", "V2", "V3"
), class = "factor"), value = c(1L, 2L, 2L, 3L, 1L, 1L, 2L, 3L, 
3L, 2L, 2L, 2L, 3L, 1L, 1L, 1L, 3L, 3L)), .Names = c("DAY", "MONTH", 
"YEAR", "pLength", "variable", "value"), row.names = c(NA, -18L
), class = "data.frame")

df2 <- structure(list(DAY = c(1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L), MONTH = c(1L, 
1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L), YEAR = c(1L, 1L, 1L, 1L, 1L, 
1L, 2L, 2L, 2L), pLength = c(0, 0.25, 1, 0, 0.5, 1, 0, 0.65, 
1), X1 = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), X2 = c(0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L), X3 = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L), X4 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L)), .Names = c("DAY", 
"MONTH", "YEAR", "pLength", "X1", "X2", "X3", "X4"), class = "data.frame", row.names = c(NA, 
-9L))

# choose colums from df2 that will be used to receive the predicted values
recvars <- c("DAY", "MONTH", "YEAR", "pLength")
rec <- df2[recvars]
recList <- dlply(rec, c("DAY", "MONTH", "YEAR", "pLength"))

# create list of models that predict the value by pLength
models <- dlply(df1, c("DAY", "MONTH", "YEAR", "variable"), function(df) 
  lm(value ~ pLength, data = df))

# get predicted values
predictions <- mdply(cbind(mod = models, df = recList), function(mod, df) {
  mutate(df, pred = predict(mod, newdata = df))
})

1 个答案:

答案 0 :(得分:1)

如果您只想将predictions转换为DFx,那么您不能这样做吗?

DFx <- predictions
DFx <- cbind(DFx,
            V1=ifelse(substr(DFx$X1,7,8)=="V1",DFx$pred,NA),
            V2=ifelse(substr(DFx$X1,7,8)=="V2",DFx$pred,NA),
            V3=ifelse(substr(DFx$X1,7,8)=="V3",DFx$pred,NA))
DFx <- DFx[,-6]   # delete "pred" column