Skip to content
Back to formatted view

Raw Message

Message-ID: <CAEd7G2eTOoB0DtNUtWi4ZKqPFwJAGB6gJ_WDjH9FB1=Y9eZ+wg@mail.gmail.com>
Date: 2013-03-13T21:43:29Z
From: Pedro Mardones
Subject: calculating column difference in a matrix

Dear R users;

Consider the following toy example:

a <- matrix(c(2,3,4,NA,NA,5,8,NA,8,NA), 5, 2)
b <- cbind(a,apply(a, 1, diff, na.rm = TRUE))

What I would like be able to get is:

c <- matrix(c(2,3,4,NA,NA,5,8,NA,8,NA,3,5,-4,8,NA), 5, 3)

i.e., for each row if both values (column 1 and 2) are NA then the
difference must return NA, but if any of those two values is different
from NA (!is.na), I would like to actually perform the difference
(something like assigning 0 instead to NA to the cell).

I'd appreciate any hint or comment

Best,

Pedro

BTW: the reason of this is that I'm trying to build an error checking
procedure for a big dataset I have