Would like to create a new data frame in R, which takes a set of rows, and combines each variation in nrow
* nrow
* ncol
format.
library(dplyr)
dat <- read.table(text =
" Animal Color Size
Cat Orange 10
Dog Black 20", header=TRUE)
Would like this output:
Animal Color Size
Cat NA NA
Cat Orange NA
Cat Orange 10
Dog NA NA
Dog Black NA
Dog Black 20
Is there a function in R which can do this -- something like expand.grid
?
expand.grid(dat$Animal, dat$Color, dat$Size) %>% arrange(Var1, Var2, Var3) #Note: this does not give the correct answer.
I'm able to create the first chunk of dat's first row using:
dat <- c("Cat", "Orange", 10)
counter <- 1
datInner <- list()
for(i in 1:length(dat)){ # loops through 3x
# i <- 3
datInner[[i]] <- dat[1:i]
counter <- counter + 1
}
library(plyr)
# Adapted from http://stackoverflow.com/questions/17308551/do-callrbind-list-for-uneven-number-of-column
plyr::rbind.fill(lapply(datInner, function(y){as.data.frame(t(y),
stringsAsFactors = FALSE)}))
# V1 V2 V3
# 1 Cat <NA> <NA>
# 2 Cat Orange <NA>
# 3 Cat Orange 10
NOTE: Will call this function type a Sequential Tree Extended Matrix (STEM). It takes a table with a tree where node depths vary, listing end nodes only, and converts it in to a table with all sequential combinations of the tree.