Wednesday, July 29, 2009

For substituting zeroes for NAs in a dataframe

For substituting zeroes for NAs in a dataframe:

x[is.na(x)] <- 0

1 comment:

  1. On a similar theme, I find the function which() very useful. It returns the position of elements in a vector or a matrix which satisfy a certain condition.

    example:
    > x <- sample(1:20,10,replace=TRUE)
    > x
    [1] 18 4 19 1 5 16 12 7 7 18
    > which(x<10)
    [1] 2 4 5 8 9
    > x[which(x<10)]
    [1] 4 1 5 7 7

    ReplyDelete