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

William Dunlap wdunlap at tibco.com
Thu Nov 11 22:19:27 CET 2010


Peter,

Your example doesn't work for me unless I
set options(stringsAsFactors=TRUE) first.
(If I do set that, then all columns of 'results'
have class "character", which I doubt the user
wants.)

> 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))
+ }
There were 36 warnings (use warnings() to see them)
> warnings()[1:5]
$`invalid factor level, NAs generated`
`[<-.factor`(`*tmp*`, ri, value = "B")

$`invalid factor level, NAs generated`
`[<-.factor`(`*tmp*`, ri, value = "2")

$`invalid factor level, NAs generated`
`[<-.factor`(`*tmp*`, ri, value = "8")

$`invalid factor level, NAs generated`
`[<-.factor`(`*tmp*`, ri, value = "-0.305558353507095")

$`invalid factor level, NAs generated`
`[<-.factor`(`*tmp*`, ri, value = "C")

> results
   X.A. X.1. X.5. X.1.43055780028799.
1     A    1    5    1.43055780028799
2  <NA> <NA> <NA>                <NA>
3  <NA> <NA> <NA>                <NA>
4  <NA> <NA> <NA>                <NA>
5  <NA> <NA> <NA>                <NA>
6  <NA> <NA> <NA>                <NA>
7  <NA> <NA> <NA>                <NA>
8  <NA> <NA> <NA>                <NA>
9  <NA> <NA> <NA>                <NA>
10 <NA> <NA> <NA>                <NA>

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com  

> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of Peter Langfelder
> Sent: Thursday, November 11, 2010 12:25 PM
> To: Noah Silverman
> Cc: r-help at r-project.org
> Subject: Re: [R] Populating then sorting a matrix and/or data.frame
> 
> 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
> 
> ______________________________________________
> 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.
> 



More information about the R-help mailing list