user11916948
user11916948

Reputation: 954

In a data frame I want to duplicate rows with a certain condition in another column

I want to duplicate all rows with treatment C 3 times. How do I do that?

A <- sample(1:100,9)
B <- sample(1:100,9)
trt <- rep(c("A", "B", "C"),3) 

df <- data.frame(trt, A, B)
> df
  trt  A  B
1   A 82 57
2   B 76  2
3   C 12 82
4   A 87 25
5   B 64 86
6   C 74 89
7   A  4 80
8   B 31 13
9   C 39 88

I have tried this but it doesn't get right!

as.data.frame(lapply(df, rep, 2))

if (df[df$trt=="C",]){
  data.frame(rep(df,3))
  }

Upvotes: 0

Views: 63

Answers (2)

rg255
rg255

Reputation: 4169

Rustic but effective, simply subset and rbind

rbind(df, df[trt =="C",], df[trt =="C",])

Here it's just joining together the original data.frame and two subsets that only contain rows where trt is C.

Upvotes: 1

Ronak Shah
Ronak Shah

Reputation: 388982

We can get the row numbers when trt == C and then use rep to repeat them

inds <- which(df$trt == "C")
df[sort(c(1:nrow(df), rep(inds, 2))), ]

#    trt  A  B
#1     A 34 58
#2     B  2 11
#3     C 57 67
#3.1   C 57 67
#3.2   C 57 67
#4     A 77 99
#5     B 37 70
#6     C 93 46
#6.1   C 93 46
#6.2   C 93 46
#7     A 61 17
#8     B 24  1
#9     C 16 54
#9.1   C 16 54
#9.2   C 16 54

Another concept can be to filter rows where trt == "C", repeat them and bind to rows where trt != "C". Using dplyr, we can implement it as

library(dplyr)
df %>%
  filter(trt == "C") %>%
  slice(rep(row_number(), 3)) %>%
  bind_rows(df %>% filter(trt != "C"))

Upvotes: 1

Related Questions