Skip to content

Assignment working differently inside ifelse()

3 messages · Stuart Luppescu, David Winsemius

#
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:
[,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.
#
On Aug 16, 2011, at 1:10 PM, Stuart Luppescu wrote:

            
Missing comma about here  
-----------------------------------------------------------------^
David Winsemius, MD
West Hartford, CT
#
On Tue, 2011-08-16 at 12:10 -0500, Stuart Luppescu wrote:
Oh, I see. ifelse() returns a value of the same length as the test
argument.