[R] Assignment working differently inside ifelse()

Stuart Luppescu slu at ccsr.uchicago.edu
Tue Aug 16 19:10:17 CEST 2011


Hello all, I need to extract rows and columns from a data frame and put
them in a matrix. In some cases, there are no rows in the data frame
meeting the selection criteria. For those rows I want to put a row of
0's in the matrix. Here's my clumsy code:

tab1.m1 <- matrix(0, nrow=2, ncol=4)

tab1.m1[1,] <- ifelse(length(as.matrix(tab1[tab1$comp==the.comp & tab1$schlid==the.schl & tab1$ext.obs==0, 4:7])) > 0, 
                          as.matrix(subset(tab1, tab1$comp==the.comp & tab1$schlid==the.schl & tab1$ext.obs==0)[4:7]),
                          0)
The row of data I want to put in the matrix is:

 tab1[tab1$comp==the.comp & tab1$schlid==the.schl & tab1$ext.obs==0,]
   schlid ext.obs comp          1          2         3         4 NA
41  22091       0    1 0.04166667 0.04166667 0.7083333 0.2083333  0

If I do this:

tab1.m1[1,] <- as.matrix(subset(tab1, tab1$comp==the.comp & tab1$schlid==the.schl & tab1$ext.obs==0)[4:7])

I get the correct values in the matrix:
> tab1.m1[1,] <- as.matrix(subset(tab1, tab1$comp==the.comp & tab1$schlid==the.schl & tab1$ext.obs==0)[4:7])
> tab1.m1
           [,1]       [,2]      [,3]      [,4]
[1,] 0.04166667 0.04166667 0.7083333 0.2083333
[2,] 0.00000000 0.00000000 0.0000000 0.0000000

but when I use the ifelse() as above, I get this:
           [,1]       [,2]       [,3]       [,4]
[1,] 0.04166667 0.04166667 0.04166667 0.04166667
[2,] 0.00000000 0.00000000 0.00000000 0.00000000

Can anyone explain this to me?
Also, is there an easier, less clumsy way to test for the existence of a
row in a data frame?
          
Thanks in advance.                
-- 
Stuart Luppescu -=- slu .at. ccsr.uchicago.edu        
University of Chicago -=- CCSR 
才文と智奈美の父 -=-    Kernel 2.6.39-gentoo-r3                
You can't expect statistical procedures to rescue
 you from poor data.    -- Berton Gunter (on
 dealing with missing values in a cluster analysis)
 R-help (April 2005)  
 
 
 
 
 


More information about the R-help mailing list