重塑数据框架

时间:2015-05-27 11:15:36

标签: r dataframe reshape

我有以下数据框

structure(list(X1 = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
3L, 3L, 3L, 3L), .Label = c("1", "2", "3"), class = "factor"), 
    V1 = c(1, NA, 1, 0, NA, NA, NA, NA, NA, NA, NA, NA), V2 = c(NA, 
    NA, NA, NA, 0, 0.25, 0.5, 1, NA, NA, NA, NA), V3 = c(NA, 
    NA, NA, NA, NA, NA, NA, NA, 0, 0.125, 0.75, 1)), .Names = c("X1", 
"V1", "V2", "V3"), row.names = c(NA, -12L), class = "data.frame")

我希望df是这样的:

structure(list(X1 = c(1L, 1L, 1L, 1L), V1 = c(1L, NA, 1L, 0L), 
    V2 = c(0, 0.25, 0.5, 1), V3 = c(0, 0.125, 0.75, 1)), .Names = c("X1", 
"V1", "V2", "V3"), class = "data.frame", row.names = c("1", "2", 
"3", "4"))

我的想法是选择列,联合NA并绑定列。但是在集合中有一些东西,有些东西告诉我这个想法不起作用/是很长的路。

问)重塑df的最佳方法是什么?

谢谢!

2 个答案:

答案 0 :(得分:0)

尝试

res <- data.frame(X1=1, sapply(df1[-1], function(x) {
              indx <- which(!is.na(x))
               x[min(indx):max(indx)]}))
res
#  X1 V1   V2    V3
#1  1  1 0.00 0.000
#2  1 NA 0.25 0.125
#3  1  1 0.50 0.750
#4  1  0 1.00 1.000

答案 1 :(得分:0)

也许您可以考虑pmaxsplit,如下所示:

as.data.frame(split(do.call(pmax, c(mydf[-1], na.rm = TRUE)), mydf$X1))
#   X1   X2    X3
# 1  1 0.00 0.000
# 2 NA 0.25 0.125
# 3  1 0.50 0.750
# 4  0 1.00 1.000

如果您确实需要一列,可以使用cbind轻松添加。{/ p>