Reputation: 55
I have a data frame as shown below let's call this as DF1
and I need to generate a new data frame as shown below
let's call this df2
df2 is nothing but, repetition of other columns except the first column A in df1
df1[1,1] is 1. so, I need to generate three rows in df2 which don't include 1 keeping the values of other columns constant correspondingly.
similarly, df1[2,1] is 2. so, I need to three rows in df2 which don't include 2 but the other three between 1 thru 4 keeping the values of other columns constant correspondingly.
please help me in doing this
thanks in advance
Upvotes: 0
Views: 195
Reputation: 51592
Here is an idea via base R,
d2 <- df[rep(rownames(df), each = 3), ]
d2$A <- unlist(lapply(df$A, function(i) setdiff(seq(4), i)))
which gives,
A B C D 1 2 2 3 4 1.1 3 2 3 4 1.2 4 2 3 4 2 1 1 2 3 2.1 3 1 2 3 2.2 4 1 2 3 3 1 3 4 2 3.1 3 3 4 2 3.2 4 3 4 2 4 1 2 3 4 4.1 2 2 3 4 4.2 3 2 3 4
NOTE: You can get rid of rownames with rownames(d2) <- NULL
Upvotes: 2