Get value of last non-NA row per column in data.table

孤街浪徒 提交于 2020-01-14 14:21:07

问题


I have a datatable where each column represents a time series, and I want to grab the last NA value per time series in a column-ordered manner. In my particular use case my data looks like this:

a   b     c
1   2     5
1   -17   9
NA  11    4
NA  57    NA
63  NA    NA

So out of this I would like to extract:

a   b    c
63  57   4

How can I accomplish this? So far I only see answers addressing the converse situation of extracting the last non-NA per row rather than per column.


回答1:


If the dataset is data.table, loop through the Subset of Data.table (.SD), subset the non-NA element (x[!is.na(x)]) and extract the last element among those with tail.

df1[, lapply(.SD, function(x) tail(x[!is.na(x)],1))]
#   a  b c
#1: 63 57 4



回答2:


For someone wishing to use only base R.

sapply(df, function(x) x[max(which(!is.na(x)))])

where

df <- data.frame(a = c(1, 1, NA, NA, 63),
                 b = c(2, -17, 11, 57, NA),
                 c = c(5, 9, 4, NA, NA))


来源:https://stackoverflow.com/questions/39934159/get-value-of-last-non-na-row-per-column-in-data-table

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!