0

I have a dataframe like this : enter image description here

I would like to dupplicate each line the number of times indicated in the column "nombreIndividus".
I tried with rep() and each = and/or time = but I can't do it.

Example :

incomeGlobalCopie <- incomeGlobalCopie[rep(1:nrow(incomeGlobalCopie),
                                           each=incomeGlobalCopie$nombreIndividus)]  

Can you help me ?
Thanks

markus
  • 25,843
  • 5
  • 39
  • 58

1 Answers1

0

Completely unelegant, but it does the trick:

names <- c("lion","tiger","flamengo")
replication <- c(4,5,3)
species <- data.frame(names, replication)
speciesCopy <- data.frame(matrix(ncol=2,nrow=0))
for(i in 1:length(species$names)){
  for(j in 1:species$replication[i]){
     speciesCopy <- rbind(speciesCopy, species[i,])
  }
}
speciesCopy
CIAndrews
  • 1,046
  • 10
  • 19
  • Hi CIAndrews, Thanks for your answer but I search a solution without loop because the dataframe is big (1160000 rows) and after duplication it will be 5800000 rows. I'm afraid that with a loop it's too long. I will try and I will tell you. Thanks. – Olivier GSCHWIND Nov 07 '18 at 07:31
  • Hi CIAndrews, actually, it works really well, but it's a bit long on a big dataframe. Thank a lot. – Olivier GSCHWIND Nov 07 '18 at 07:52