5

问题

我发现自己使用从对象层次结构中reshape2::melt获取单个“长” 。但是,结果的列名具有标记为“L1”、“L2”等的列表层次结构级别。但是,由于这些级别有意义,我想给这些列赋予有意义的名称。最好的方法是什么?可以使用单个调用来完成吗?data.framelistdata.framemelt

我不喜欢meltor reshape2,所以我对其他方法或包持开放态度。

当前设置

假设我们有一个data.frame对象的分层列表,如下所示:

library(reshape2)
x <- structure(list(cyl_6 = structure(list(gear_3 = structure(list( mpg = 1:2, qsec = 3:4), .Names = c("mpg", "qsec"), row.names = c(NA, -2L), class = "data.frame"), gear_4 = structure(list(mpg = 5:6, qsec = 7:8), .Names = c("mpg", "qsec"), row.names = c(NA, -2L), class = "data.frame")), .Names = c("gear_3", "gear_4")), cyl_8 = structure(list(gear_3 = structure(list(mpg = 9:10, qsec = 11:12), .Names = c("mpg", "qsec"), row.names = c(NA, -2L), class = "data.frame"), gear_4 = structure(list(mpg = 13:14, qsec = 15:16), .Names = c("mpg", "qsec"), row.names = c(NA, -2L), class = "data.frame")), .Names = c("gear_3", "gear_4"    ))), .Names = c("cyl_6", "cyl_8"))

当我使用 时melt(x),我得到列名“L1”表示汽缸数,“L2”表示齿轮数。我希望专栏分别改为“ cylinders”和“ gears”。

mx <- melt(x)

这是 的输出head(mx)。我希望它只说“L1”和“L2”:

1> head(mx)
  variable value     L2    L1
1      mpg     1 gear_3 cyl_6
2      mpg     2 gear_3 cyl_6
3     qsec     3 gear_3 cyl_6
4     qsec     4 gear_3 cyl_6
5      mpg     5 gear_4 cyl_6
6      mpg     6 gear_4 cyl_6

所以,我求助于手动设置“L1”和“L2”:

names(mx)[3:4] <- c("gears", "cylinders")

期望的输出

这是所需的最终列名设置。我希望能够在不手动重置 mx 的“名称”作为单独步骤的情况下实现这一目标。

1> head(mx)
   variable value  gears cylinders
1       mpg     1 gear_3     cyl_6
2       mpg     2 gear_3     cyl_6
3      qsec     3 gear_3     cyl_6
4      qsec     4 gear_3     cyl_6
5       mpg     5 gear_4     cyl_6
6       mpg     6 gear_4     cyl_6
4

2 回答 2

3

你可以试试

library(tidyr)
res <- unnest(x, sex)
head(res)
#   sex  Hair   Eye value
#1 MALE Black Brown    32
#2 MALE Brown Brown    53
#3 MALE   Red Brown    10
#4 MALE Blond Brown     3
#5 MALE Black  Blue    11
#6 MALE Brown  Blue    50

对于更新后的问题,使用unnest. unnest需要反复调用

library(dplyr)
dN <- unnest(lapply(x, unnest, gear), cylinders) %>%
                         gather(variable, value, mpg:qsec)
head(dN,3)
#  cylinders   gear variable value
#1     cyl_6 gear_3      mpg     1
#2     cyl_6 gear_3      mpg     2
#3     cyl_6 gear_4      mpg     5
于 2015-04-15T19:14:05.583 回答
2
library('reshape2')
x <- structure(list(cyl_6 = structure(list(gear_3 = structure(list( mpg = 1:2, qsec = 3:4), .Names = c("mpg", "qsec"), row.names = c(NA, -2L), class = "data.frame"), gear_4 = structure(list(mpg = 5:6, qsec = 7:8), .Names = c("mpg", "qsec"), row.names = c(NA, -2L), class = "data.frame")), .Names = c("gear_3", "gear_4")), cyl_8 = structure(list(gear_3 = structure(list(mpg = 9:10, qsec = 11:12), .Names = c("mpg", "qsec"), row.names = c(NA, -2L), class = "data.frame"), gear_4 = structure(list(mpg = 13:14, qsec = 15:16), .Names = c("mpg", "qsec"), row.names = c(NA, -2L), class = "data.frame")), .Names = c("gear_3", "gear_4"    ))), .Names = c("cyl_6", "cyl_8"))

查看源代码,Ls 是硬编码的,因此如果不像现在这样执行额外的步骤,就无法即时更改它们。

reshape2:::melt.list
# function (data, ..., level = 1) 
# {
#   parts <- lapply(data, melt, level = level + 1, ...)
#   result <- rbind.fill(parts)
#   names <- names(data) %||% seq_along(data)
#   lengths <- vapply(parts, nrow, integer(1))
#   labels <- rep(names, lengths)
#   label_var <- attr(data, "varname") %||% paste("L", level, sep = "")
#   result[[label_var]] <- labels
#   result
# }

可以做的是稍微更改此函数并添加一个新参数,以便您可以随意命名它们:

meltList <- function (data, ..., level = 1, nn) {
  require('reshape2')
  '%||%' <- function (a, b) if (!is.null(a)) a else b
  parts <- lapply(data, melt, level = level + 1, ...)
  result <- plyr::rbind.fill(parts)
  names <- names(data) %||% seq_along(data)
  lengths <- vapply(parts, nrow, integer(1))
  labels <- rep(names, lengths)
  label_var <- attr(data, "varname") %||% paste("L", level, sep = "")
  result[[label_var]] <- labels
  if (!missing(nn))
    names(result)[grep('^L\\d+', names(result))] <- nn
  result
}

前任

head(meltList(x, nn = c('gears','cylinders')))
#   variable value  gears cylinders
# 1      mpg     1 gear_3     cyl_6
# 2      mpg     2 gear_3     cyl_6
# 3     qsec     3 gear_3     cyl_6
# 4     qsec     4 gear_3     cyl_6
# 5      mpg     5 gear_4     cyl_6
# 6      mpg     6 gear_4     cyl_6

这也适用于更多嵌套列表:

y <- list(x = x, y = x)
head(melt(y))
#   variable value     L3    L2 L1
# 1      mpg     1 gear_3 cyl_6  x
# 2      mpg     2 gear_3 cyl_6  x
# 3     qsec     3 gear_3 cyl_6  x
# 4     qsec     4 gear_3 cyl_6  x
# 5      mpg     5 gear_4 cyl_6  x
# 6      mpg     6 gear_4 cyl_6  x

对比

head(meltList(y, nn = c('gears','cylinders','variable')))
#   variable value  gears cylinders variable
# 1      mpg     1 gear_3     cyl_6        x
# 2      mpg     2 gear_3     cyl_6        x
# 3     qsec     3 gear_3     cyl_6        x
# 4     qsec     4 gear_3     cyl_6        x
# 5      mpg     5 gear_4     cyl_6        x
# 6      mpg     6 gear_4     cyl_6        x
于 2015-04-15T22:13:44.663 回答