Liondancer
Liondancer

Reputation: 16469

Dataframes based off the value in the last row

How can I subset a CSV in R based on the value in each column of the last row?

For example, I read in a CSV file called mnist.csv and it is of dimension 400x600.

columns   1 2 3 4 .......... 600
rows:   1
        2
        3
        4
        .
        .
        .
      400 0 0 0 1 1 1 ...3 3 3

I want to create dataframes that will only have the column and rows where the value is 0 in the last row and another dataframe where the value is 1 in the last row

Upvotes: 0

Views: 46

Answers (1)

r2evans
r2evans

Reputation: 160447

Using sample data, looking instead for 4 instead of 0:

mtcars[,mtcars[nrow(mtcars),] == 4L,drop=FALSE]

Disecting that:

  • nrow(mtcars) allows us to index on the "last" of each column
  • mtcars[...,] returns a 1-D matrix, the last values of each column
  • ... == 4L returns a logical vector, corresponding to each column that matches
  • mtcars[,...,drop=FALSE] just does the column-subsetting; the drop=FALSE is to guard against the matrix being simplified to a vector when only one column matches, as is the case in mtcars[,mtcars[nrow(mtcars),] == 2L].

Upvotes: 1

Related Questions