重塑数据框

Reshape a data frame

我有以下数据框

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 并绑定列。但是集合中有 na,​​有些东西告诉我这个想法行不通/这是很长的路要走。

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

谢谢!

尝试

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

也许你可以只考虑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

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