I have the following dataframe and I want to get a vector that contains the last non-NA row of each column:
df <- data.frame(a = 1:5, b = 6:10, c = c(rep(NA, 5)))
df <- rbind(df, rep(NA, ncol(df)), rep(NA, ncol(df)))
df[2,] <- NA
df$b[6]<-8
df
a b c
1 1 6 NA
2 NA NA NA
3 3 8 NA
4 4 9 NA
5 5 10 NA
6 NA 8 NA
7 NA NA NA
The vector I am looking for would be as follows:
c(5,6,0)
How can I achieve this using base R? So far, I run the following formula...
apply(row(df) + 0 * df, 2, function(x) max(x, na.rm = TRUE))
...but it throws me the following warning message:
Warning message: In max(x, na.rm = TRUE) : no non-missing arguments to max; returning -Inf