2013-08-02 26 views
3

我有一個寬格式表,前三行用於描述表中顯示的數據。例如:熔解多行

Company:    | Company A | Company B | Company C |  | Company N 
Data source:   | Budget  | Actual  | Budget  | ... | ... 
Currency:    | USD  | EUR  | USD  |  | ... 
Indicator: 
Sales     500   1000   1500  ...  ... 
Gross Income    200   300   400  ...  ... 
...      ...   ...   ...  ...  ... 
Indicator J    ...   ...   ...  ... 

我想將它與以下佈局重塑長格式:

Indicator | Company | Currency | Data Source | Value 
Sales | Company A | USD | Budget  | 500 
Sales | Company B | EUR | Actual  | 1000 
...  | ... | ... | ...  | ... 

我試着reshape2包融化,但沒能轉換行2和3以變​​量

dput(AAA) 
structure(list(V1 = structure(c(1L, 8L, 2L, 5L, 7L, 4L, 3L, 6L 
), .Label = c("Company:", "Currency:", "EBITDA", "Gross Income", 
"Indicator:", "Net Income", "Sales", "Source:"), class = "factor"), 
    V2 = structure(c(7L, 6L, 8L, 1L, 2L, 5L, 3L, 4L), .Label = c("", 
    "1000", "150", "25", "300", "Budget", "Company A", "USD"), class = "factor"), 
    V3 = structure(c(7L, 6L, 8L, 1L, 2L, 5L, 3L, 4L), .Label = c("", 
    "1500", "175", "30", "400", "Actual", "Company B", "USD"), class = "factor"), 
    V4 = structure(c(7L, 6L, 8L, 1L, 3L, 5L, 2L, 4L), .Label = c("", 
    "185", "2000", "45", "500", "Budget", "Company C", "EUR"), class = "factor"), 
    V5 = structure(c(7L, 6L, 8L, 1L, 3L, 5L, 2L, 4L), .Label = c("", 
    "195", "2500", "50", "700", "Actual", "Company D", "EUR"), class = "factor")), .Names = c("V1", 
"V2", "V3", "V4", "V5"), class = "data.frame", row.names = c(NA, 
-8L)) 
+3

請'輸入'數據,以便它可以複製 – Metrics

回答

2

這是一個解決方案,涉及轉置您的數據和做一些清洗。其餘的是通過「熔融」來完成:

AAA <- structure(list(V1 = structure(c(1L, 8L, 2L, 5L, 7L, 4L, 3L, 6L 
), .Label = c("Company:", "Currency:", "EBITDA", "Gross Income", 
       "Indicator:", "Net Income", "Sales", "Source:"), class = "factor"), 
       V2 = structure(c(7L, 6L, 8L, 1L, 2L, 5L, 3L, 4L), .Label = c("", 
                      "1000", "150", "25", "300", "Budget", "Company A", "USD"), class = "factor"), 
       V3 = structure(c(7L, 6L, 8L, 1L, 2L, 5L, 3L, 4L), .Label = c("", 
                      "1500", "175", "30", "400", "Actual", "Company B", "USD"), class = "factor"), 
       V4 = structure(c(7L, 6L, 8L, 1L, 3L, 5L, 2L, 4L), .Label = c("", 
                      "185", "2000", "45", "500", "Budget", "Company C", "EUR"), class = "factor"), 
       V5 = structure(c(7L, 6L, 8L, 1L, 3L, 5L, 2L, 4L), .Label = c("", 
                      "195", "2500", "50", "700", "Actual", "Company D", "EUR"), class = "factor")), .Names = c("V1", 
                                             "V2", "V3", "V4", "V5"), class = "data.frame", row.names = c(NA, 
                                                            -8L)) 
# transpose data 
dft <- data.frame(t(AAA), stringsAsFactors=FALSE) 

require(reshape2) 
# set colnames 
colnames(dft) <- dft[1, ] 
dft <- dft[-1, ] 

# remove empty indicator col 
dft[ , 4] <- NULL 

# melt data 
melt(dft, id.vars=c('Company:', 'Source:', 'Currency:'), variable.name='Indicator:') 

# Company: Source: Currency: Indicator: value 
# 1 Company A Budget  USD  Sales 1000 
# 2 Company B Actual  USD  Sales 1500 
# 3 Company C Budget  EUR  Sales 2000 
# 4 Company D Actual  EUR  Sales 2500 

也許你會需要一些更多的清洗(現在每山坳是性格,也許調換前還設置了colnames ...)。

+0

+1。也許類似下面的內容也是有用的:'data.frame(lapply(x1,function(x)type.convert(as.character(x))))'(其中「x1」是你的'melt'命令的結果)。 – A5C1D2H2I1M1N2O1R2T1