Ale
Ale

Reputation: 11

loops with data from a matrix

i have a database that i have concentrate in a matrix called DB as follow:

         PN        time.state.2 STATUS
   [1,] 6954010001            0    3.0
   [2,] 6954010001            3    3.5
   [3,] 6954010001            6    3.5
   [4,] 6954010001            9    3.5
   [5,] 6954010001           12    3.5

in which there are many subjects and for each of them more than one row is registered (are different visits of patients for which a STATUS is registered).

I would like create a for loop that create an object called "progression" if the same patient increase own value of STATUS in subsequent visits.

I don't understand how to assign to index "i" the PN code of the patients to permit that when finish with a patient go to the further.

For example for one patient with these SCORE values at each time-point highlighted by time.state.2 object i would like that patient is considered PROGRESSED when his SCORE value increase of 1-point compared with the first timepoint of that patient (first visit in hospital). Further this PROGRESSION has to be confirmed in the subsequent visit (for this patient at time 6 STATUS reach 4.0 (1-point higher than the first visit which was 3.0) and this value is confirmed in the subsequent visit so the PROGRESSION is confirmed.)

         PN        time.state.2 STATUS  PROGRESSION
   [1,] 6954010001            0    3.0            0
   [2,] 6954010001            3    3.5            0
   [3,] 6954010001            6    4.0            1
   [4,] 6954010001            9    4.0            0
   [5,] 6954010001           12    4.5            0
   [6,] 6954010001           15    4.5            0

I would like also that PROGRESSION for each patient is 1 only the first time and possibly drop (for that patient) the subsequent visits after his progression. For example:

         PN        time.state.2 STATUS  PROGRESSION
   [1,] 6954010001            0    3.0            0
   [2,] 6954010001            3    3.5            0
   [3,] 6954010001            6    4.0            1
   [4,] 6954010002            0    6.0            0
   [5,] 6954010002            3    6.0            0

when the first patient stop when PROGRESSION=1.

Upvotes: 1

Views: 113

Answers (1)

Roland
Roland

Reputation: 132696

I believe you want something like this:

#create data
DF <- read.table(text="         PN        time.state.2 STATUS
   [1,] 6954010001            0    3.0
   [2,] 6954010001            3    3.5
   [3,] 6954010001            6    3.5
   [4,] 6954010001            9    3.5
   [5,] 6954010001           12    3.5
   [6,] 6954010002            0    3.0
   [7,] 6954010002            3    3.0
   [8,] 6954010002            6    3.5
   [9,] 6954010002            9    3.5
   [10,] 6954010002          12    3.5",header=TRUE)

#you claim to have a matrix
m <- as.matrix(DF)

#turn the matrix into a data.frame
DF <- as.data.frame(m)
rownames(DF) <- NULL

#use package plyr to split according to patient, 
#apply function, and combine back
library(plyr)
#calculate the cumulative sum of differences in STATUS
#put a 0 in front, since there can be no progress at the first time point
DF <- ddply(DF,.(PN),transform,progress=c(0,cumsum(diff(STATUS))))

print(DF)
#            PN time.state.2 STATUS progress
# 1  6954010001            0    3.0      0.0
# 2  6954010001            3    3.5      0.5
# 3  6954010001            6    3.5      0.5
# 4  6954010001            9    3.5      0.5
# 5  6954010001           12    3.5      0.5
# 6  6954010002            0    3.0      0.0
# 7  6954010002            3    3.0      0.0
# 8  6954010002            6    3.5      0.5
# 9  6954010002            9    3.5      0.5
# 10 6954010002           12    3.5      0.5

Edit after clarification:

DF <- read.table(text="         PN        time.state.2 STATUS
[1,] 6954010001            0    3.0
[2,] 6954010001            3    3.5
[3,] 6954010001            6    4.0
[4,] 6954010001            9    3.5
[5,] 6954010001           12    6.0
[6,] 6954010002            0    3.0
[7,] 6954010002            3    4.0
[8,] 6954010002            6    4.0
[9,] 6954010002            9    6.0
[10,] 6954010002          12    6.0",header=TRUE)

rownames(DF) <- NULL

DF <- ddply(DF,.(PN),transform,progress=(STATUS-STATUS[1])>=1 & 
                                        (c(STATUS[-1],FALSE)-STATUS[1])>=1)

DF <- ddply(DF,.(PN),function(x) {x$progress[x$progress][-1] <- FALSE; x})

#            PN time.state.2 STATUS progress
# 1  6954010001            0    3.0    FALSE
# 2  6954010001            3    3.5    FALSE
# 3  6954010001            6    4.0    FALSE
# 4  6954010001            9    3.5    FALSE
# 5  6954010001           12    6.0    FALSE
# 6  6954010002            0    3.0    FALSE
# 7  6954010002            3    4.0     TRUE
# 8  6954010002            6    4.0    FALSE
# 9  6954010002            9    6.0    FALSE
# 10 6954010002           12    6.0    FALSE

Upvotes: 1

Related Questions