2016-04-15 55 views
0

我試圖以熔化子類別表(CSV),但由於某種原因不能獲得與melt.array期望的結果[R融化小類

我:

Region,Sender,Afghanistan,,,, 
,,Adult,,Minor,, 
,,Male ,Female,Male,Female, 
EEA,Austria,165,8,6,10, 
.... 

需要:

Reg Send Rec   Age Gender Value 
EEA Austria Afghanistan Adult Male 165 
EEA Austria Afghanistan Adult Female 8 
EEA Austria Afghanistan Adult Male 6 
EEA Austria Afghanistan Adult Female 10 
.... 

並將非常感謝一些幫助!

+0

是第一臺CSV文件?你可以用逗號粘貼它嗎? – chinsoon12

+0

對不起,它在評論部分 – chinsoon12

+0

不是很可讀!你是對的!我改變了原來的代碼。 – Thomas

回答

0

不要有因爲結構,但一個非常優雅的方式動物園:: na.locf可能是你在找什麼

library(zoo) 
data <- structure(list(V1 = structure(c(3L, 1L, 1L, 2L), .Label = c("", 
                    "EEA", "Region"), class = "factor"), V2 = structure(c(3L, 1L, 
                                  1L, 2L), .Label = c("", "Austria", "Sender"), class = "factor"), 
         V3 = structure(c(3L, 2L, 4L, 1L), .Label = c("165", "Adult", 
                    "Afghanistan", "Male "), class = "factor"), V4 = structure(c(1L, 
                                   1L, 3L, 2L), .Label = c("", "8", "Female"), class = "factor"), 
         V5 = structure(c(1L, 4L, 3L, 2L), .Label = c("", "6", "Male", 
                    "Minor"), class = "factor"), V6 = structure(c(1L, 1L, 3L, 
                                2L), .Label = c("", "10", "Female"), class = "factor")), .Names = c("V1", 

data[1:3,][data[1:3,]==""] <- NA 
data <- na.locf(zoo(data), fromLast=F) 
df <- cbind(as.character(data$V1[4]), as.character(temp$V2[4]), 
      data.frame(na.locf(zoo(t(data)), fromLast=F)[-(1:2),])) 
setNames(df, c("Reg","Send","Rec","Age","Gender", "Value"))