Last active
December 27, 2016 06:14
-
-
Save JakeConway/dd40fa305960c6fa0db8 to your computer and use it in GitHub Desktop.
expression to UpSetR converter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fromExpression <- function(vd){ | |
vd <- list(vd) | |
intersections <- lapply(vd, function(x) strsplit(names(unlist(x)), "&")) | |
intersections <- lapply(intersections[[1]], function(x) unlist(as.list(x))) | |
sets <- unique(unlist(intersections)) | |
data <- na.omit(data.frame(matrix(NA, ncol = length(sets)))) | |
names(data) <- sets | |
counts <- lapply(vd, function(x) unlist(x)) | |
names(counts[[1]]) <- NULL | |
counts[[1]] <- as.numeric(counts[[1]]) | |
for(i in seq(intersections)) { | |
cols <- match(names(data), intersections[[i]]) | |
cols[!is.na(cols)] <- 1 | |
cols[is.na(cols)] <- 0 | |
cols <- rep(cols, times = counts[[1]][i]) | |
cols <- matrix(cols, ncol = length(sets), byrow = T) | |
cols <- data.frame(cols) | |
names(cols) <- sets | |
data <- rbind(data, cols) | |
} | |
return(data) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment