0

I have the following data set concerning the use of a service. People are allowed to check in and out of the service so there is a entered service date and a left service date. On another occassion further on, they may enter the service again and leave it after some days.

I want to be able to know for each use of the service (represented by a row) by a person, what is the number of times he/she has used the service in the previous year.

What I have tried

I computed an service use index to denote the nth time a service has been used. Next I made use of the index to compute the days since the previous service use. From there on I'm stuck. I'm not sure how I should go about looking back.

I am quite stuck and would appreciate any tips on how to proceed. I wanted to use lapply to subset each person into its own dataframe but after which how do I look back?

Thanks.

Dataset

read.table("http://dl.dropbox.com/u/822467/dataset.csv", sep = ",", header = TRUE)

To further illustrate what I need

The following is the data from subject 22. The subject has a total of 5 service usage. For each service usage sans the 1st one, I would look back at the 1 year preceding his entry to the service. E.g. For the 2nd usage of the service, I would look at the entry date, which is 14/08/2009. I would then look at the previous service usage to see how many fall into a window between 15/08/2008 to 14/08/2009. I would need to do this for all the instances of service usage for every subject.

SubID   Entered_Service Left_Service    Service_Usage_Index Days_Since_Last_Service_Use_Ended
22      09/06/2008      13/06/2008      1                   NA
22      14/08/2009      17/08/2009      2                   427
22      21/03/2010      22/03/2010      3                   216
22      25/03/2010      31/03/2010      4                   3
22      21/06/2010      24/06/2010      5                   82

1 Answer 1

0

It's not the most elegant solution but i'd proceed as follows (if I understood correctly your problem):

data <- read.table("http://dl.dropbox.com/u/822467/dataset.csv", sep = ",", header = TRUE)
# first, define your dates as dates so you can compare them
strptime(data[,2],format="%d/%m/%Y") -> entry
strptime(data[,3],format="%d/%m/%Y") -> exit
strptime("31/12/2011",format="%d/%m/%Y") -> end
strptime("01/01/2011",format="%d/%m/%Y") -> start
# then select all rows from 2011
data[(entry<=end & entry>=start) | (exit<=end & exit>=start),] -> data2011
# then see how many rows correspond to each user ID
summary(as.factor(data2011$SubID))

Edit

Based on the same idea, I hope it will do the trick:

data <- read.table("http://dl.dropbox.com/u/822467/dataset.csv", sep = ",", header = TRUE)
data[!is.na(data[,1]),]->data
result <- rep(NA,length=nrow(data))

for(i in unique(data$SubID)){
# Loop through each subject
    data[data$SubID==i,]->temp
    if(nrow(temp)>1){
        for(j in 2:nrow(temp)){
            strptime(temp[j,2],format="%d/%m/%Y") -> end
            end - 365*24*3600 -> start
            # There might be a better way to substract a year to a date but I don't know it...
            strptime(temp[,2],format="%d/%m/%Y") -> entry
            strptime(temp[,3],format="%d/%m/%Y") -> exit
            nrow(temp[(entry<end & entry>=start) | (exit<end & exit>=start),]) -> result[data$SubID==i & data[,2]==temp[j,2]]
            }
        }
    }

result -> data$result
1
  • apologies, i think i wasn't clear enough on the first try. I've edited my question to better reflect what i need. Thanks for your help though.
    – RJ-
    Jun 29, 2012 at 14:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.