Holding the base, you get where you are going:
#create data set foo <- data.frame(Var1=c("2|1","1|2","","",""),Var2=c("","","","","1|2"), Var3=c("", "", "1|2","2|1","") ,Var4=c("","","", "1|2", ""))
or similarly:
lens <- sapply(foo, function(x) sum(x != "")) x <- as.matrix(foo) data.frame(GF = x[x!= ""], GF1 = rep(colnames(foo), lens))
source share