While Eric's solution should work for your case, here is a slightly more 
general version where the number of replications is dependent on a column in 
the data frame. In the example, the number of replications required is the 
ceiling of the number of 30 day intervals between start date and end date, 
which are already available in the data frame. The key function is f - this 
takes a row index (of df) and replicates as many times as determined by the 
value in the column num_int for that row. To modify f, you need to replace 
num_int by the appropriate column name in your data set. f is then mapped to 
each row number of df and the replicated indices are stored in rowindex_rep as 
a vector. The final step is to subset df which repeats the rows appropriately. 

df <- data.frame(
  order_id = c(1, 2),
  start_date = c(as.Date("2017-05-01"), as.Date("2017-08-01")),
  end_date = c(as.Date("2017-07-06"), as.Date("2017-09-15"))
)
df$diff_days <- as.integer(df$end_date - df$start_date)
df$num_int <- ceiling(df$diff_days / 30)

f <- function(rowindex) {
  rep(rowindex, each = df[rowindex, "num_int"])
}

rowindex_rep <- unlist(Map(f, 1:nrow(df)))
df2 <- df[rowindex_rep, ]


Regards,
radmuzom



From: R-help <r-help-boun...@r-project.org> on behalf of Eric Berger 
<ericjber...@gmail.com>
Sent: Sunday, July 8, 2018 3:21 PM
To: catalin roibu
Cc: R Project Help
Subject: Re: [R] replicate rows
  

Hi Catalin,

This should work. I set the number of repetitions and sample sizes as
variables so it would be clear how to modify for your actual case.

nreps    <- 3
sampSize <- 2
w <- unlist( lapply(1:nreps, function(i) {
rep(paste("R",i,sep=""),sampSize) } ) )
aa2 <- cbind( as.data.frame(aa), w)

HTH,
Eric


On Sun, Jul 8, 2018 at 11:44 AM, catalin roibu <catalinro...@gmail.com>
wrote:

> Dear R users,
>
> I want to replicate sampled rows in data frame. The sampling results must
> be in this form:
>
>   a   b     Rep
> [1,] 3 4.0 R1
> [2,] 6 8.0 R1
> [3,] 1 0.1 R2
> [4,] 6 8.0 R2
> [5,] 1 0.1 R3
> [6,] 5 7.0 R3
>
> I have a code but I didn't succeed to insert to rep column.
>
> This is my code:
> a<-c(1,2,3,4,5,6)
> b<-c(0.1, 0.2, 4, 6, 7, 8)
> ab<-cbind(a, b)
> x<-replicate(3, sample(1:nrow(ab), 2))
> aa<-ab[x, ]
>
> Please help me to solve that problem!
>
> Thank you very much!
>
> Best regards!
>
> Catalin
>
> --
>
> -
> -
> Catalin-Constantin ROIBU
> ​
> Lecturer PhD, Forestry engineer
> Forestry Faculty of Suceava
> Str. Universitatii no. 13, Suceava, 720229, Romania
> office phone      +4 0230 52 29 78, ext. 531
> mobile phone    +4 0745 53 18 01
> FAX:                +4 0230 52 16 64
> silvic.usv.ro <http://www.usv.ro/>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/
> posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide  http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
    
______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to