r - Multiple outputs from single menu -


suppose have data frame this:

class  sex  score      m    90      f    90      f    85      m    85      m    80      m    70      f    70      m    60 b      f    90 b      m    90 b      m    75 b      f    70 

and want single menu selects class , sex , gets average. right on real data frame i'm using 2 menus

i <- menu(c("a","b"), graphics=true, title="choose class") j <- menu(c("m","f"), graphics=true, title="choose sex") df.1 <- df.1[df.1$class==i, ] df.1 <- df.1[df.1$sex==j, ] 

but when there many more variables class , sex seems annoying click multiple menus when selected in 1 window. possible in r?

this modified basic idea create intersection of of options show , use single menu.

dat <- read.table(textconnection("class  sex  score      m    90      f    90      f    85      m    85      m    80      m    70      f    70      m    60 b      f    90 b      m    90 b      m    75 b      f    70 "), header = true)  vals <- interaction(dat$class, dat$sex) opts <- as.character(unique(vals)) choice <- menu(opts, graphics = true, title = "choose class.sex") dat[vals == opts[choice],] 

and here idea wrapped function

# data - dataset subset # cols - either character vector names of columns #        or numeric vector column numbers # graphics - logical. should menu graphical? subsetmenu <- function(data, cols, graphics = true){     if(is.numeric(cols)){         colnames <- colnames(data)[cols]     }else{         colnames <- cols     }      vals <- interaction(data[,cols])     opts <- as.character(unique(vals))     title <- paste("choose", paste0(colnames, collapse = "."))      choice <- menu(opts, graphics = graphics, title = title)     data[vals == opts[choice],] }  df1 <- subsetmenu(dat, c("class", "sex"), graphics = t) df2 <- subsetmenu(dat, c("class", "sex"), graphics = f) df3 <- subsetmenu(dat, 1:2) df4 <- subsetmetu(mtcars, c("cyl", "gear")) 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -