Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select last value in a row, by row

Tags:

dataframe

r

I have a data frame where each row is a vector of values of varying lengths. I would like to create a vector of the last true value in each row.

Here is an example data frame:

df <- read.table(tc <- textConnection("
   var1    var2    var3    var4
     1       2       NA      NA
     4       4       NA      6
     2       NA      3       NA                
     4       4       4       4              
     1       NA      NA      NA"), header = TRUE); close(tc)

The vector of values I want would therefore be c(2,6,3,4,1).

I just can't figure out how to get R to identify the last value.

Any help is appreciated!

like image 707
jslefche Avatar asked Sep 23 '11 17:09

jslefche


People also ask

How do you select the last item in a row in Excel?

Note: To select the very last cell in a row or column, press END, and then press the RIGHT ARROW key or the DOWN ARROW key.

How do I find the last 5 values in a row?

You can use AVERAGE, OFFSET, and COUNT functions to average last 5 values in columns. The OFFSET function is used here to return an array of a range with rows, columns, height, and weight. The AVERAGE function then averages the elements in the array.

How do I find the first and last value in a row in Excel?

When you are faced with a table that is scattered with values among empty cells, you might need to know the first (left-most) or last (right-most) value in that row. =INDEX(D2:H2,MATCH(TRUE,INDEX((D2:H2<>0),0),0)) , copied down as needed. =INDEX(2:2,MATCH(9.99999E+307,2:2)) , copied down as needed.


2 Answers

Do this by combining three things:

  • Identify NA values with is.na
  • Find the last value in a vector with tail
  • Use apply to apply this function to each row in the data.frame

The code:

lastValue <- function(x)   tail(x[!is.na(x)], 1)

apply(df, 1, lastValue)
[1] 2 6 3 4 1
like image 136
Andrie Avatar answered Oct 23 '22 03:10

Andrie


Here's an answer using matrix subsetting:

df[cbind( 1:nrow(df), max.col(!is.na(df),"last") )]

This max.col call will select the position of the last non-NA value in each row (or select the first position if they are all NA).

like image 1
Frank Avatar answered Oct 23 '22 03:10

Frank