获取 data.table 中每列最后一个非 NA 行的值 [英] Get value of last non-NA row per column in data.table
问题描述
我有一个 datatable
,其中每一列代表一个时间序列,我想以列顺序的方式获取每个时间序列的最后一个 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
所以我想从中提取:
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屋!