R熔体子类别

R熔体子类别,r,reshape2,melt,R,Reshape2,Melt,我正在尝试用子类别融化一个表(csv),但由于某些原因,无法使用melt.array获得所需的结果 我有: Region,Sender,Afghanistan,,,, ,,Adult,,Minor,, ,,Male ,Female,Male,Female, EEA,Austria,165,8,6,10, .... 需要: 如果能得到一些帮助,我将不胜感激 由于结构原因,没有一种非常优雅的方式,但是zoo::na.locf可能就是你想要的 library(zoo) data <- stru

我正在尝试用子类别融化一个表(csv),但由于某些原因,无法使用melt.array获得所需的结果

我有:

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


如果能得到一些帮助,我将不胜感激

由于结构原因,没有一种非常优雅的方式,但是zoo::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"))
图书馆(动物园)

数据是csv文件的第一个表吗?你能用逗号把它粘贴进去吗?很抱歉,它在评论部分的可读性不是很好。你是对的!我更改了原始代码。很抱歉,现在输出有点混乱,我有点忘乎所以了。现在我们有了它。有什么想法吗?我正试图绘制一大组驱逐出境数据,但我并不擅长数据争论!
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"))