I have a data.frame df
and I want that every row in this df
is duplicated lengthTime
times and that a new column is added that counts from 1 to lengthTime
for each row in df
.
I know, it sounds pretty complicated, but what I basically want is to apply expand.grid
to df
. Here is an ugly workaround and I have the feeling that there most be an easier solution (maybe even a base-R function?):
df <- data.frame(ID = rep(letters[1:3], each=3), CatA = rep(1:3, times = 3), CatB = letters[1:9]) lengthTime <- 3 nrRow <- nrow(df) intDF <- df for (i in 1:(lengthTime - 1)) { df <- rbind(df, intDF) } df$Time <- rep(1:lengthTime, each=nrRow)
I thought that I could just use expand.grid(df, 1:lengthTime)
, but that does not work. outer
did not bring any luck either. So does anyone know a good solution?
It's been a while since this question was posted, but I recently came across it looking for just the thing in the title, namely, an expand.grid
that works for data frames. The posted answers address the OP's more specific question, so in case anyone is looking for a more general solution for data frames, here's a slightly more general approach:
expand.grid.df <- function(...) Reduce(function(...) merge(..., by=NULL), list(...)) # For the example in the OP expand.grid.df(df, data.frame(1:lengthTime)) # More generally df1 <- data.frame(A=1:3, B=11:13) df2 <- data.frame(C=51:52, D=c("Y", "N")) df3 <- data.frame(E=c("+", "-")) expand.grid.df(df1, df2, df3)
Why not just something like df[rep(1:nrow(df),times = 3),]
to extend the data frame, and then add the extra column just as you have above, with df$Time <- rep(1:lengthTime, each=nrRow)
?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With