[R] Coding columns for survival analysis

Alexander Shenkin ashenkin at ufl.edu
Fri Apr 13 22:53:23 CEST 2012


Hello Folks,

I have 5 columns for thousands of tree records that record whether that
tree was alive or dead.  I want to recode the columns such that the cell
reads "found" when a live tree is first observed, "alive" for when a
tree is found alive and is not just found, and "mort" when it was
previously alive but is now dead.

Given the following:

    > tree_live = data.frame(tree =
c("tree1","tree2","tree3","tree4","tree5"), live1 = c(0,0,0,1,1), live2
= c(0,0,1,1,0), live3 = c(0,1,1,0,0), live4 = c(1,1,0,0,0), live5 = c(1,
0, 0, 0, 0))

       tree live1 live2 live3 live4 live5
    1 tree1     0     0     0     1     1
    2 tree2     0     0     1     1     0
    3 tree3     0     1     1     0     0
    4 tree4     1     1     0     0     0
    5 tree5     1     0     0     0     0

I would like to end up with the following:

    > tree_live_recode

      live1 live2 live3 live4 live5
    1    NA    NA    NA found alive
    2    NA    NA found alive  mort
    3    NA found alive  mort     0
    4 found alive  mort     0     0
    5 found  mort     0     0     0

I've accomplished the recode in the past, but only by going over the
dataset multiple times in messy and inefficient fashion.  I'm wondering
if there are concise and efficient ways of going about it?

(I haven't been using the Survival package for my analyses, but I'm
starting to look into it.)

Thanks,
Allie



More information about the R-help mailing list