Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

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

$
0
0

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))
  }

Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>