获取data.table中每列的最后一个非NA行的值 [英] Get value of last non-NA row per column in data.table
问题描述
我有一个数据表
,其中每一列代表一个时间序列,我想以列排序的方式获取每个时间序列的最后一个NA值。在我的特定用例中,我的数据如下所示:
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
我该怎么做?到目前为止,我只看到解决从行而不是从列提取最后一个非NA的相反情况的答案。
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.
推荐答案
如果数据集是 data.table
,遍历Data.table的子集( .SD
),将非NA元素( x [!is.na(x)]
)并提取具有 tail
的元素中的最后一个元素。
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
这篇关于获取data.table中每列的最后一个非NA行的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!