2012-03-07 114 views
0

我寫了兩個包裝函數用於投射和融化,以將我的數據從長條形碼 變爲寬幅形式,反之亦然。然而,我仍然在努力使功能 reshape_wide它把長形式的數據變成寬的形式。R重塑數據從長到寬,反之亦然

以下是我的示例函數和運行它的代碼。我創建了一個寬的 格式的dummy 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") 

這裏是我的右輸出鏈接,當我運行上面的代碼:

http://pastebin.com/ej8F9GnL

什麼是錯的,在列B型出現5次,而不是3倍,因爲它應該是。 你有相同的data.frame嗎?

這裏是sessionInfo()

> 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 
+0

這適用於我的機器。你使用的是什麼版本的重塑包?也許將'sessionInfo()'的結果添加到你的問題中。 – Andrie 2012-03-07 15:44:06

回答

0

的例子不能工作: 由於ID和類型不構成主鍵 (即,因爲有幾行用相同的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") 
+0

它現在。這是沒有「主鍵」的問題!大。非常感謝! – user969113 2012-03-08 18:19:59

0

問題可能是這裏,r輸出:

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它會失敗。

+0

使用ave的兩種方法給出完全相同的結果。我不認爲這是問題。我認爲這是我使用dcast功能的方式。然而,當數據平衡時,從寬 - >長 - >寬的工作重塑工作!請嘗試使用以下data.frame:data < - data.frame(ID = rep(「K」,6),Type = c代表( 「A」,3),代表( 「B」,3)), \t \t \t \t X = 1:6,Y = 7:12,Z = 13時18) – user969113 2012-03-07 17:05:36