Skip to content

na.action

2 messages · Tim Smith, Tony Plate

#
Maybe this does what you want:

 > x <- as.matrix(read.table("clipboard"))
 > x
   V1 V2 V3 V4
1 NA  0  0  0
2  0 NA  0 NA
3  0  0 NA  2
4  0  0  2 NA
 > rowSums(x==2, na.rm=T)
1 2 3 4
0 0 1 1
 >

There's probably at least 5 or 6 other quite sensible ways of doing 
this, but this is probably the fastest (and the least versatile).

A more general building block is the sum() function, as in:

 > sum(x[3,]==2, na.rm=T)
[1] 1
 >

The key is the use of the 'na.rm=T' argument value.

hope this helps,

Tony Plate
Tim Smith wrote: