gpt4 book ai didi

r - 如何在R中按组删除前导和尾随NA的行

转载 作者:行者123 更新时间:2023-12-04 17:29:37 26 4
gpt4 key购买 nike

我需要删除包含 NA 的行,但前提是它们是前导(尾随),即在变量出现任何数据之前(之后)。这非常类似于:
How to find (not replace) leading NAs, gaps, and final NAs in data.table columns by category
和:
How delete leading and trailing rows by condition in R?

但是,我需要按照变量“ID”进行分组。我将在后面的步骤中估算 NA 的数据。

这同样适用于尾随的 NA。

初始 data.frame 如下所示:

df1<-data.frame(ID=(rep(c("C1001","C1008","C1009","C1012"),each=17)),
Year=(rep(c(1996:2012),4)),x1=(floor(runif(68,20,75))),x2=
(floor(runif(68,1,100))))

#Introduce leading / tailing NAs

df1[1:5,3]<-NA
df1[18:23,4]<-NA
df1[35:42,4]<-NA
df1[49:51,3]<-NA
df1[66:68,3]<-NA


#introduce "gap"- NAs
set.seed(123)
df1$x1[rbinom(68,1,0.1)==1]<-NA
df1$x2[rbinom(68,1,0.1)==1]<-NA

输出很长。这是为了正确区分“间隙”-NA 和“前导/尾随” NA
head(df1,10)

ID Year x1 x2
1 C1001 1996 NA 40
2 C1001 1997 NA 88
3 C1001 1998 NA 37
4 C1001 1999 NA 29
5 C1001 2000 NA 17
6 C1001 2001 42 18
7 C1001 2002 20 48
8 C1001 2003 30 26
9 C1001 2004 66 22
10 C1001 2005 32 67


输出应按 ID 组去除前导 NA(参见上面的第 1:5 行)。或者下例中的 18:23 行:
df1[18:28,]

ID Year x1 x2
18 C1008 1996 33 NA
19 C1008 1997 26 NA
20 C1008 1998 NA NA
21 C1008 1999 51 NA
22 C1008 2000 31 NA
23 C1008 2001 44 NA
24 C1008 2002 NA 56
25 C1008 2003 47 70
26 C1008 2004 39 91
27 C1008 2005 55 62
28 C1008 2006 40 43

最终输出应该是这样的(当然取决于随机的 NA!):
      ID Year x1 x2
6 C1001 2001 42 18
7 C1001 2002 20 48
8 C1001 2003 30 26
9 C1001 2004 66 22
10 C1001 2005 32 67
11 C1001 2006 NA 5
12 C1001 2007 24 70
13 C1001 2008 33 35
14 C1001 2009 60 41
15 C1001 2010 66 82
16 C1001 2011 47 91
17 C1001 2012 41 28
24 C1008 2002 NA 56
25 C1008 2003 47 70
26 C1008 2004 39 91
27 C1008 2005 55 62
28 C1008 2006 40 43
29 C1008 2007 39 54
30 C1008 2008 49 6
31 C1008 2009 NA 26
32 C1008 2010 NA 40
33 C1008 2011 42 20
34 C1008 2012 34 83
44 C1009 2005 51 96
45 C1009 2006 66 96
46 C1009 2007 37 NA
47 C1009 2008 58 26
48 C1009 2009 34 22
52 C1012 1996 51 78
53 C1012 1997 70 17
54 C1012 1998 69 41
55 C1012 1999 35 47
56 C1012 2000 37 86
57 C1012 2001 74 92
58 C1012 2002 54 NA
59 C1012 2003 71 67
60 C1012 2004 45 95
61 C1012 2005 42 52
62 C1012 2006 56 58
63 C1012 2007 28 34
64 C1012 2008 51 35
65 C1012 2009 33 2

谢谢一堆!

最佳答案

这是一个 依赖 rleid 的解决方案仅删除领先的 NA:

library(data.table)
dt <- as.data.table(df1)

dt[,
.SD[!(rleid(x1) %in% c(1, max(rleid(x1))) & is.na(x1)) &
!(rleid(x2) %in% c(1, max(rleid(x2))) & is.na(x2))],
by = ID
]

自动处理多列,假设它们都以 x 开头你可以这样做:
dt[dt[, Reduce('&',
lapply(.SD, function(x) !(rleid(x) %in% c(1, max(rleid(x1))) & is.na(x)))),
by = ID,
.SDcols = grep('x', names(dt))]$V1
]
# or using .SD as before

dt[,
.SD[Reduce('&', lapply(.SD, function(x) !(rleid(x) %in% c(1, max(rleid(x1))) & is.na(x)))),
.SDcols = grep('x', names(dt))],
by = ID
]

或与 相同的想法:
library(dplyr)
library(data.table)

df1%>%
group_by(ID)%>%
filter_at(vars(starts_with('x')), all_vars(!(is.na(.) & rleid(.) %in% c(1, max(rleid(.))))))

结果为 42 行:
# A tibble: 42 x 4
# Groups: ID [4]
ID Year x1 x2
<fct> <int> <dbl> <dbl>
1 C1001 2001 25 54
2 C1001 2002 28 50
3 C1001 2003 35 94
4 C1001 2004 52 34
5 C1001 2005 60 47
6 C1001 2006 NA 9
7 C1001 2007 67 86
8 C1001 2008 58 40
9 C1001 2009 61 73
10 C1001 2010 28 18
# ... with 32 more rows

关于r - 如何在R中按组删除前导和尾随NA的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58570110/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com