[R] Build a dataframe row by row?
Gavin Simpson
gavin.simpson at ucl.ac.uk
Wed Aug 5 13:04:56 CEST 2009
On Wed, 2009-08-05 at 10:28 +1000, Remko Duursma wrote:
> > Nice, but I need a few columns for the data. Don't know how to do this with
> > the method you suggest.
>
> For each iteration, you make a vector of your 'data', in the example
> it is a vector of length 3.
> Each of the elements in this vector will become a column in the dataframe.
>
> If that is not what you mean, can you give a specific example?
>
> remko
If you know that there will be 10 rows then why not allocate the DF
initially and fill in by row during the loop?
nc <- 3
nr <- 10
df <- data.frame(matrix(NA, ncol = nc, nrow = nr))
for(i in seq_len(nr)) {
## Noah can fill in the "cells" as he goes with various results
## the below is just to illustrate
df[i,1] <- rnorm(1)
df[i,2] <- runif(1)
df[i,3] <- rpois(1, 4)
}
df
G
>
>
>
> > -N
> >
> > On 8/4/09 4:57 PM, Remko Duursma wrote:
> >>
> >> Hi Noah,
> >>
> >> there are a few ways to do this. Easiest is to keep adding an element
> >> to a list, and then make it into a dataframe at the end, like this:
> >>
> >> resultlis<- list()
> >> for(i in 1:10){
> >>
> >> # your calculations here
> >> datavec<- runif(3)
> >>
> >> resultlis[[i]]<- datavec
> >>
> >> }
> >> resultdfr<- as.data.frame(do.call("rbind", resultlis))
> >>
> >>
> >> greetings,
> >> Remko
> >>
> >> -------------------------------------------------
> >> Remko Duursma
> >> Post-Doctoral Fellow
> >>
> >> Centre for Plants and the Environment
> >> University of Western Sydney
> >> Hawkesbury Campus
> >> Richmond NSW 2753
> >>
> >> Dept of Biological Science
> >> Macquarie University
> >> North Ryde NSW 2109
> >> Australia
> >>
> >> Mobile: +61 (0)422 096908
> >> www.remkoduursma.com
> >>
> >>
> >>
> >> On Wed, Aug 5, 2009 at 9:41 AM, Noah Silverman<noah at smartmediacorp.com>
> >> wrote:
> >>
> >>>
> >>> Hi,
> >>>
> >>> Time for another of my "newbie" questions.
> >>>
> >>> Is it possible to build up a data.frame "row by row" as I go
> >>>
> >>> I'm going to be running a bunch of experiments (many in a loop) to test
> >>> different things. I'm using AUC as my main performance measure.
> >>>
> >>> My thought was to add a row to a data.frame for each iteration and then
> >>> have
> >>> a nice summary report at the end.
> >>>
> >>> I found how to import, export, and create from vectors. I haven't been
> >>> able
> >>> to find how just make an empty one and add rows as I go.
> >>>
> >>> ______________________________________________
> >>> R-help at r-project.org mailing list
> >>> 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 at r-project.org mailing list
> 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.
--
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
Dr. Gavin Simpson [t] +44 (0)20 7679 0522
ECRC, UCL Geography, [f] +44 (0)20 7679 0565
Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk
Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/
UK. WC1E 6BT. [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: This is a digitally signed message part
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090805/d9cdf538/attachment-0002.bin>
More information about the R-help
mailing list