I have a data frame that looks somewhat like this:
df <- data.frame(0:2, 1:3, 2:4, 5:7, 6:8, 2:4, 0:2, 1:3, 2:4) colnames(df) <- rep(c(\'a\', \'b\', \
Use %in% and some unlisting
%in%
zz <- lapply(unique(names(df)), function(x,y) as.vector(unlist(df[which(y %in% x)])),y=names(df)) names(zz) <- unique(names(df)) as.data.frame(zz) a b c 1 0 1 2 2 1 2 3 3 2 3 4 4 5 6 2 5 6 7 3 6 7 8 4 7 0 1 2 8 1 2 3 9 2 3 4