r - Subset a Data Frame Based on All Combinations and Sub-combinations of Factor Variables -


i need subset data.frame based on combinations sub-combinations of multiple columns of factor variables. additionally number of columns factor variables may change method needs flexible in accepting different numbers of attributes. can figure out how create combinations of variables in simple example don't have way subset data.frame efficiently. thoughts?

#setup example data.frame <- c("a", "b", "b", "b", "e") b <- c("b", "c", "b", "b", "f") c <- c("c", "d", "b", "b", "g") df <- data.table(a = a, b = b, c = c)   #build data.frame of unique combos subset on  df_unique <- df[!duplicated(df), ] df_combos <- data.table() for(i in 1:ncol(df_unique)){   for(x in 1:ncol(df_unique)){     df_sub <- df_unique[,i:x, = f]     df_combos <- rbind(df_combos, df_sub, fill = t)   } }  df_combos <- df_combos[!duplicated(df_combos), ] rm(df_unique)   #create loop build subsets combos_out <- data.table() for(i in 1:nrow(df_combos)){   df_combos_sub <- df_combos[i, ]   df_combos_sub <- df_combos_sub[,which(unlist(lapply(df_combos_sub, function(x)!all(is.na(x))))),with=f]   df_sub <- merge(df, df_combos_sub, = colnames(df_combos_sub))     #interesting code here performs analysis on subsets } 


Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -