Skip to content
Back to formatted view

Raw Message

Message-ID: <586FDDFF.3000109@sapo.pt>
Date: 2017-01-06T18:12:15Z
From: Rui Barradas
Subject: About populating a dataframe in a loop
In-Reply-To: <CAN5afy9QKtxJQyCxtcGXOL=vPCX0PDMreG=sjRF2GggrL8GhCQ@mail.gmail.com>

Hello,

Works with me:

set.seed(6574)

pre.mat = data.frame()
for(i in 1:10){
     mat.temp = data.frame(x = rnorm(5), A = sample(LETTERS, 5, TRUE))
     pre.mat = rbind(pre.mat, mat.temp)
}

nrow(pre.mat)  # should be 50


Can you give us an example that doesn't work?

Rui Barradas

Em 06-01-2017 18:00, lily li escreveu:
> Hi R users,
>
> I have a question about filling a dataframe in R using a for loop.
>
> I created an empty dataframe first and then filled it, using the code:
> pre.mat = data.frame()
> for(i in 1:10){
>      mat.temp = data.frame(some values filled in)
>      pre.mat = rbind(pre.mat, mat.temp)
> }
> However, the resulted dataframe has not all the rows that I desired for.
> What is the problem and how to solve it? Thanks.
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at 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.
>