Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(dplyr)
- dat <- read.table(text =
- " Animal Color Size
- Cat Orange 10
- Dog Black 20", header=TRUE)
- Animal Color Size
- Cat NA NA
- Cat Orange NA
- Cat Orange 10
- Dog NA NA
- Dog Black NA
- Dog Black 20
- expand.grid(dat$Animal, dat$Color, dat$Size) %>% arrange(Var1, Var2, Var3) #Note: this does not give the correct answer.
- 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement