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!
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.
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.
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.
Do this by combining three things:
NA
values with is.na
tail
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
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With