[R] replicate rows
Eric Berger
er|cjberger @end|ng |rom gm@||@com
Sun Jul 8 11:51:29 CEST 2018
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 <catalinroibu using 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 using 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]]
More information about the R-help
mailing list