Find the number of columns between the first and last non NA value

I have the following dataframe,

a1=c(9,8,rep(NA,5))
a2=c(3,NA,3,NA,3,NA,4)
a3=c(11,6,7,NA,5,NA,NA)
k<-as.data.frame(rbind(a1,a2,a3))

I would like to add a column that indicates the number of columns from the first until the last non NA value. That is for the first row the value in this additional column would be 2, for the second row it would be 7, and for the last it would be 5.



Read more here: https://stackoverflow.com/questions/68490039/find-the-number-of-columns-between-the-first-and-last-non-na-value

Content Attribution

This content was originally published by vasile at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: