[R] Populating then sorting a matrix and/or data.frame

Peter Langfelder peter.langfelder at gmail.com
Thu Nov 11 21:24:38 CET 2010


On Thu, Nov 11, 2010 at 11:33 AM, Noah Silverman
<noah at smartmediacorp.com> wrote:
> Still doesn't work.
>
> When using rbind to build the data.frame, it get a structure mostly full of
> NA.
> The data is correct, so something about pushing into the data.frame is
> breaking.
>
> Example code:
> results <- data.frame()
>
> for(i in 1:n){
>    #do all the work
>    #a is a test label. b,c,d are numeric.
>    results <- rbind(results, c(a,b,c,d))
> }

Works for me:

results <- data.frame()

n = 10
for(i in 1:n){
   a = LETTERS[i];
   b = i;
   c = 3*i + 2
   d = rnorm(1);
   results <- rbind(results, c(a,b,c,d))
}


> results

   X.A. X.1. X.5. X.0.142223304589023.
1     A    1    5    0.142223304589023
2     B    2    8    0.243612305595176
3     C    3   11    0.476795513990516
4     D    4   14      1.0278220664213
5     E    5   17    0.916608672305205
6     F    6   20     1.61075985995586
7     G    7   23    0.370423691258896
8     H    8   26  -0.0528603547004191
9     I    9   29    -2.07888666920403
10    J   10   32    -1.87980721733655

Maybe there's something wrong with the calculation you do?

Peter



More information about the R-help mailing list