[R] Re moving Numeric,0 from dataframe

Marc Schwartz marc_schwartz at comcast.net
Thu Jan 8 00:15:10 CET 2009


on 01/07/2009 02:34 PM PDXRugger wrote:
> I realize i am breaking the posting rules by not posting sample code but i
> tried building some sample test code for this problem based on my working
> code and it wasnt producing what i wanted so hopefully a brief explanation
> and my result will allow you guys enough information to offer some advice. 
> My result:
> 
> allTAZprobs TAZS
> [1,] Numeric,0   640 
> [2,] 0.4385542   641 
> [3,] 0.2876207   642 
> [4,] Numeric,0   643 
> [5,] Numeric,0   649 
> [6,] Numeric,0   650 
> [7,] 0.7543349   652 
> [8,] Numeric,0   654 
> 
> is a dataframe that is built after about 4 iterative processes of looking up
> some numbers in different tables and plugging them into the next process. 
> The "Numeric,0" result is acheived becuase that missing value didnt get
> through a "greater than" comparison with a random number, that part is fine. 
> What i want the result to be is:
> 
> [1,] 0.4385542   641 
> [2,] 0.2876207   642 
> [3,] 0.7543349   652 
> 
> throwing out all of the rows with a "Numeric 0" and keeping the rest.  I
> havent been able to find a way to do this while keeping the two colums
> matched.  Not sure if i will have to alter things farther up in my code or
> if i can just do some sort of unlist() function to clear out the unwanted
> rows.  Again i apoligize for no working code and realize the breach of
> protocol but i hope my question is clear and i hope someone can help, Cheers
> 
> JR

It would be helpful to see the output of:

  str(YourResultObject)

as I suspect that it is a matrix (actually perhaps a list with dims) and
not a data frame, given the row indices being output.

If the output of the above looks something like this, with the result
object being called 'MAT':

> str(MAT)
List of 16
 $ : num(0)
 $ : num 0.439
 $ : num 0.288
 $ : num(0)
 $ : num(0)
 $ : num(0)
 $ : num 0.754
 $ : num(0)
 $ : num 640
 $ : num 641
 $ : num 642
 $ : num 643
 $ : num 649
 $ : num 650
 $ : num 652
 $ : num 654
 - attr(*, "dim")= int [1:2] 8 2
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:2] "allTAZprobs" "TAZS"


Then one approach to subsetting it, would be:

> subset(as.data.frame(MAT), allTAZprobs > 0)
  allTAZprobs TAZS
2   0.4385542  641
3   0.2876207  642
7   0.7543349  652


Another possibility would be:

> MAT[sapply(MAT[, 1], length) > 0, ]
     allTAZprobs TAZS
[1,] 0.4385542   641
[2,] 0.2876207   642
[3,] 0.7543349   652


But it all depends upon the actual structure of your result object.

A better approach would be to do a check at each step in the simulation
to see if there is a result or not and assign NA if not. Then you can
use something like na.omit() or similar on the final object.

HTH,

Marc Schwartz




More information about the R-help mailing list