【问题标题】:R reshape data from long to wide and vice versaR重塑数据从长到宽,反之亦然
【发布时间】:2012-03-25 03:34:36
【问题描述】:

我为 cast 和 melt 编写了两个包装函数来将我的数据从 long 到宽形式,反之亦然。但是,我仍然在使用该功能 reshape_wide 将数据从长格式转换为宽格式。

这是我的示例函数以及运行它的代码。我在宽范围内创建了一个虚拟 data.frame 我使用 reshape_long 函数将其重塑为长格式,然后使用 reshape_wide 函数将其转换回原始宽格式。但是,由于我无法理解的原因,重塑失败了。看来dcast的公式是错误的。

reshape_long <- function(data, identifiers) {
    data_long <- melt(data, id.vars = identifiers, 
                            variable.name="name", value.name="value")
    data_long$value <- as.numeric(data_long$value)
    data_long <- data_long[!is.na(data_long$value), ]
    return(data_long)
}

reshape_wide <- function(data, identifiers, name) {
    if(is.null(identifiers)) {
        formula_wide <- as.formula(paste(paste(identifiers,collapse="+"), 
                                   "series ~ ", name))      
    } else {
        formula_wide <- as.formula(paste(paste(identifiers,collapse="+"), 
                                   "+ series ~ ", name))
    }
    series <- ave(1:nrow(data), data$name, FUN=function(x) { seq.int(along=x) }) 
    data <- cbind(data, series) 
    data_wide <- dcast(data, formula_wide, value.var="value")
    data_wide <- data_wide[,!(names(data_wide) %in% "series")]
    return(data_wide)
}


data <- data.frame(ID = rep("K", 6), Type = c(rep("A", 3), rep("B", 3)),
                   X = c(NA,NA,1,2,3,4), Y = 5:10, Z = c(NA,11,12,NA,14,NA))
data <- reshape_long(data, identifiers = c("ID", "Type"))
data
reshape_wide(data, identifiers = c("ID", "Type"), name="name")

当我运行上面的代码时,这是一个指向我的 R 输出的链接:

http://pastebin.com/ej8F9GnL

问题在于 B 列中出现了 5 次而不是应有的 3 次。 你得到相同的data.frame吗?

这是 sessionInfo() 的 R 输出

> sessionInfo()
R version 2.14.0 (2011-10-31)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)

locale:
[1] C

attached base packages:
[1] grid      stats     graphics  grDevices utils     datasets  methods  
[8] base     

other attached packages:
 [1] reshape2_1.2.1       outliers_0.14        lme4_0.999375-42    
 [4] Matrix_1.0-1         gregmisc_2.1.2       gplots_2.10.1       
 [7] KernSmooth_2.23-7    caTools_1.12         bitops_1.0-4.1      
[10] gtools_2.6.2         gmodels_2.15.1       gdata_2.8.2         
[13] lattice_0.20-0       dataframes2xls_0.4.5 RankProd_2.26.0     
[16] R.utils_1.9.3        R.oo_1.8.3           R.methodsS3_1.2.1   
[19] xlsx_0.3.0           xlsxjars_0.3.0       rJava_0.9-2         
[22] rj_1.0.0-3          

loaded via a namespace (and not attached):
[1] MASS_7.3-16   nlme_3.1-102  plyr_1.6      rj.gd_1.0.0-1 stats4_2.14.0
[6] stringr_0.5   tools_2.14.0 

【问题讨论】:

  • 这适用于我的机器。您使用的是什么版本的 reshape 包?也许将sessionInfo() 的结果添加到您的问题中。

标签: r long-integer reshape


【解决方案1】:

该示例无法运行: 因为 ID 和 Type 不构成主键 (即,由于有几行具有相同的 id 和类型), 当数据以高格式放置时,您不再知道 如果两个值来自同一行。

另外,我不确定您要如何处理您的 series 列, 但它似乎不起作用。

library(reshape2)
d <- data.frame(
  ID = rep("K", 6), 
  Type = c(rep("A", 3), rep("B", 3)),
  X = c(NA,NA,1,2,3,4), 
  Y = 5:10, 
  Z = c(NA,11,12,NA,14,NA)
)
d$row <- seq_len(nrow(d))  # (row,ID,Type) is now a primary key
d
d1 <- reshape_long(d, identifiers = c("row", "ID", "Type"))
d1
dcast(d1, row + ID + Type ~ name) # Probably what you want
reshape_wide(d1, identifiers = c("row", "ID", "Type"), name="name")

【讨论】:

  • 现在可以使用了。这是没有“主键”的问题!伟大的。非常感谢!
【解决方案2】:

问题可能出在这里:

series <- ave(1:nrow(data), data$name, FUN=function(x) { seq.int(along=x) }) 

应该改掉在函数中使用“$”的习惯,因为它不会解释传递的值。使用 "[[" 并且不要引用参数:

series <- ave(1:nrow(data), data[[name]], FUN=function(x) { seq.int(along=x) }) 

在此示例中,它不会产生影响,因为 name == "name",但如果您尝试将它与 name 的任何其他值一起使用,它将失败。

【讨论】:

  • 两种使用 ave 的方式给出的结果完全相同。我不认为那是问题所在。我认为这是我使用 dcast 功能的方式。但是,当数据平衡时,从宽 -> 长 -> 宽的重塑工作!尝试使用以下 data.frame: data
猜你喜欢
  • 2012-12-25
  • 2012-12-01
  • 1970-01-01
  • 2016-03-20
  • 1970-01-01
  • 2019-11-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多