jc2525
jc2525

Reputation: 141

how can you subset last values of a data frame in R?

I have a large data frame that sorta looks like this in its basic form...

subject session 
1         1
2         1
3         1
1         2
2         2
3         2
1         3
2         3
3         3
1         4
2         4
3         4
1         5
2         5
3         5

How can I subset just the last 2 sessions of the data frame (i.e. sessions 4 and 5)?

Note: The total number of sessions may change between scenarios but I will always only want to look at the last 2 sessions.

So code like this would not suffice all scenarios...

data <- subset(data, session == c(4,5))

Upvotes: 2

Views: 241

Answers (2)

ThomasIsCoding
ThomasIsCoding

Reputation: 101209

Here is another base R option

> subset(df, session >= sort(unique(session), decreasing = TRUE)[2])
   subject session
10       1       4
11       2       4
12       3       4
13       1       5
14       2       5
15       3       5

or

> subset(df, session >= -sort(-unique(session))[2])
   subject session
10       1       4
11       2       4
12       3       4
13       1       5
14       2       5
15       3       5

Upvotes: 1

akrun
akrun

Reputation: 887038

With more than one element, use %in% instead of ==. Get the unique 'session' values and return the last two with tail, create the logical expression with %in% for subsetting

subset(data, session %in% tail(unique(session), 2))

As @Greg mentioned in the comments, if it should be based on the ordered 'session' and the column values are not sorted, the do the sort on the unique elements before applying the tail

subset(data, session %in% tail(sort(unique(session)), 2))

Upvotes: 6

Related Questions