R中的数据准备:n长度到堆积列的行保留名称

时间:2014-02-07 01:18:42

标签: r

我有:

df1<-structure(list(name = structure(1:5, .Label = c("name1", "name2", 
"name3", "name4", "name5"), class = "factor"), x1 = c(1L, 2L, 
4L, 5L, 8L), x2 = c(1L, 2L, 4L, 5L, 8L), x3 = c(1L, 2L, 4L, 5L, 
8L), x4 = c(1L, 2L, 4L, 5L, 8L), x5 = c(NA, 3L, NA, 6L, NA), 
    x6 = c(NA, 3L, NA, 6L, NA), x7 = c(NA, 3L, NA, 6L, NA), x8 = c(NA, 
    3L, NA, 6L, NA), x9 = c(NA, NA, NA, 7L, NA), x10 = c(NA, 
    NA, NA, 7L, NA), x11 = c(NA, NA, NA, 7L, NA), x12 = c(NA, 
    NA, NA, 7L, NA)), .Names = c("name", "x1", "x2", "x3", "x4", 
"x5", "x6", "x7", "x8", "x9", "x10", "x11", "x12"), class = "data.frame", row.names = c(NA, 
-5L))

我想:

df2<-structure(list(name = structure(c(1L, 2L, 2L, 3L, 4L, 4L, 4L, 
5L), .Label = c("name1", "name2", "name3", "name4", "name5"), class = "factor"), 
    y1 = 1:8, y2 = 1:8, y3 = 1:8, y4 = 1:8), .Names = c("name", 
"y1", "y2", "y3", "y4"), class = "data.frame", row.names = c(NA, 
-8L))

帮助!

df1具有不同长度的行,但所有序列都是4个单元格(它们是坐标)。 不能通过编写函数或plyr来完成它......

2 个答案:

答案 0 :(得分:2)

我不确定这是否是一个好的答案,但有效

df11<-df1[,1:5]
names(df11)<-c("name","y1","y2","y3","y4")

df12<-df1[,c(1,6:9)]
names(df12)<-c("name","y1","y2","y3","y4")


df13<-df1[,c(1,10:13)]
names(df13)<-c("name","y1","y2","y3","y4")

df12<-rbind(df11, df12,df13)
df2<-na.omit(df12)

如果超过13,你总是可以使用循环来获取每4个结尾,或者ncol 所以,使用循环

n<-ncol(df1)
newdf=data.frame(name=as.character(), y1=as.integer(), y2=as.integer(), y3=as.integer(), y4=as.integer())
for (x in seq(2,n,by=4)){
  subdf<-df1[,c(1,as.integer(x):as.integer(x+3))]
  names(subdf)<-c("name","y1","y2","y3","y4")
  newdf<-rbind(newdf,subdf)
}
df2.loop<-na.omit(newdf)

答案 1 :(得分:1)

更短,更容易扩展:

intm <- apply(df1[-1],1,function(x) data.frame(y=matrix(x,ncol=4,byrow=TRUE)))
na.omit(data.frame(name=rep(df1$name,sapply(intm,nrow)),do.call(rbind,intm)))

#   name y.1 y.2 y.3 y.4
#1 name1   1   1   1   1
#2 name2   2   2   2   2
#3 name2   3   3   3   3
#4 name3   4   4   4   4
#5 name4   5   5   5   5
#6 name4   6   6   6   6
#7 name4   7   7   7   7
#8 name5   8   8   8   8