Extract last non-missing value in row with data.table

前端 未结 5 896
囚心锁ツ
囚心锁ツ 2021-01-04 04:03

I have a data.table of factor columns, and I want to pull out the label of the last non-missing value in each row. It\'s kindof a typical max.col situation, bu

5条回答
  •  感动是毒
    2021-01-04 04:36

    Here is a one liner base R approach:

    sapply(split(dat, seq(nrow(dat))), function(x) tail(x[!is.na(x)],1))
    #  1   2   3   4   5   6   7   8 
    #"u" "q" "w" "h" "r" "t" "e" "t" 
    

提交回复
热议问题