Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (50.2k points)

I want to select the particular client sessions based on a date. I am having a data frame with all sessions of clients and I want to make two new df's: one with the first session of each client, and one with the second session of each client. The sessions are randomly generated numbers, just like the client id's (for example I adjusted them to 1, 2, and 3).

My data looks like:

Client id     Session id     Date

    1             95738        13-03-2019

    1             61718        18-03-2019

    1             81289        19-03-2019

    1             89239        20-03-2019

    2             91298        13-02-2019

    2             12794        15-02-2019

    2             10083        16-02-2019

    3             90138        03-02-2019

    3             23128        06-02-2019

I want to have the output for the df with only the first session like:

Client id     Session id     Date

    1             95738        13-03-2019

    2             91298        13-02-2019

    3             90138        03-02-2019

And for the df with the second sessions like:

Client id     Session id     Date

    1             61718        18-03-2019

    2             12794        15-02-2019

    3             23128        06-02-2019

1 Answer

0 votes
by (108k points)

You can simply use the below code in the baseR:

index <- order(mydata[,1])[!duplicated(sort(mydata[,1]))] # Finds first occurance

mydata[index,]

The above code gives:

  Clientid Sessionid       Date

1        1     95738 13-03-2019

5        2     91298 13-02-2019

8        3     90138 03-02-2019

mydata[(index+1),]

This gives:

  Clientid Sessionid       Date

2        1     61718 18-03-2019

6        2     12794 15-02-2019

9        3     23128 06-02-2019

Data:

mydata <- read.table(text="Clientid     Sessionid     Date

    1             95738        13-03-2019

    1             61718        18-03-2019

    1             81289        19-03-2019

    1             89239        20-03-2019

    2             91298        13-02-2019

    2             12794        15-02-2019

    2             10083        16-02-2019

    3             90138        03-02-2019

    3             23128        06-02-2019",header=T)

If you are a beginner and want to know more about R then do refer to the following R programming tutorial

...