如何删除仅在R中包含NA的列表元素?
要删除仅包含NA的列表元素,可以将Filter函数与Negate函数一起使用。例如,如果我们有一个名为LIST的列表,其中包含一个或多个具有所有NA的元素,则可以使用以下命令删除这些元素:
Filter(Negate(anyNA),LIST)
例1
考虑以下列表-
List1<-list(x1=rnorm(50,5,0.32),x2=rnorm(50,25,3.2),x3=rnorm(50,8,5),x4=rnorm(50,1,0.34),x5=rep(NA,50))输出结果List1
$x1[1] 4.334321 4.889232 4.381391 4.674231 5.101552 5.399012 4.954232 4.947509
[9] 5.251860 5.077353 4.739267 5.413977 5.439906 4.877928 4.717069 5.045421
[17] 5.295415 5.259428 4.837964 5.378886 4.147506 5.101758 4.930767 5.473703
[25] 4.939583 5.096297 5.554951 5.085816 5.267561 4.788085 5.457247 5.002054
[33] 5.111992 4.924476 5.535720 4.695025 5.283118 5.229327 4.745801 4.703609
[41] 5.198589 4.827950 5.062294 4.925777 4.534809 5.082817 4.895610 5.080772
[49] 5.343944 5.342646
$x2
[1] 27.09805 23.79097 23.08307 25.79603 31.40680 31.29629 27.44809 22.14314
[9] 29.87338 25.24330 21.92249 21.25923 24.04215 20.54472 28.21643 30.62872
[17] 27.54715 27.37227 27.85388 29.37858 29.59009 26.17834 19.80734 21.04692
[25] 26.35492 22.45115 22.40127 17.12838 28.54318 25.55538 25.96085 23.04064
[33] 24.25620 29.51622 25.12644 21.21018 28.24440 26.29723 25.38056 27.47820
[41] 29.38003 29.36458 28.53252 31.96616 28.31610 28.16186 26.51348 26.02747
[49] 27.08449 21.91069
$x3
[1] 11.6201614 11.7248762 13.3374679 4.3256780 8.1032854 6.1235977
[7] 11.7805026 7.4148509 3.5650701 15.8647983 14.8382621 15.6786356
[13] 11.5860206 0.0198924 2.9176717 16.3342936 7.3713642 14.9845785
[19] 5.2901857 9.8382025 -2.2347269 16.1264894 16.4666958 7.4579946
[25] 4.4730358 14.7587509 4.5682974 9.8460002 0.3835374 18.2413788
[31] 7.5890881 0.8431373 16.4580390 14.2566923 6.0304444 6.5470628
[37] 22.3208480 5.2943139 12.7842887 1.5344631 6.7964196 -0.9742034
[43] 7.3987924 5.5863990 3.0760514 11.6147581 9.6301556 5.3435136
[49] 0.8412404 20.0820351
$x4
[1] 1.4423468 1.7144002 0.6814712 1.6969592 1.2746219 1.1918903 0.8507043
[8] 1.2746324 0.3850853 0.1225640 0.8898047 0.8581539 1.3253551 0.7939156
[15] 0.7914138 0.7263540 1.3253416 0.9616639 1.4976244 0.3633276 1.7613419
[22] 0.9318393 1.1681829 0.7034077 1.3640640 1.1932273 0.8706002 0.5524266
[29] 0.6099138 0.2450496 0.7283081 0.6691275 0.6675042 1.0578633 0.7729999
[36] 1.2503530 0.9114667 1.3385308 1.2386105 0.5828654 1.4406876 1.0419542
[43] 1.7966151 1.1035424 1.0303867 0.4414917 0.8689440 1.4014934 1.1922928
[50] 1.2072652
$x5
[1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
[26] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
删除List1中仅包含NA的元素-
筛选器(Negate(anyNA),List1)
$x1[1] 4.334321 4.889232 4.381391 4.674231 5.101552 5.399012 4.954232 4.947509
[9] 5.251860 5.077353 4.739267 5.413977 5.439906 4.877928 4.717069 5.045421
[17] 5.295415 5.259428 4.837964 5.378886 4.147506 5.101758 4.930767 5.473703
[25] 4.939583 5.096297 5.554951 5.085816 5.267561 4.788085 5.457247 5.002054
[33] 5.111992 4.924476 5.535720 4.695025 5.283118 5.229327 4.745801 4.703609
[41] 5.198589 4.827950 5.062294 4.925777 4.534809 5.082817 4.895610 5.080772
[49] 5.343944 5.342646
$x2
[1] 27.09805 23.79097 23.08307 25.79603 31.40680 31.29629 27.44809 22.14314
[9] 29.87338 25.24330 21.92249 21.25923 24.04215 20.54472 28.21643 30.62872
[17] 27.54715 27.37227 27.85388 29.37858 29.59009 26.17834 19.80734 21.04692
[25] 26.35492 22.45115 22.40127 17.12838 28.54318 25.55538 25.96085 23.04064
[33] 24.25620 29.51622 25.12644 21.21018 28.24440 26.29723 25.38056 27.47820
[41] 29.38003 29.36458 28.53252 31.96616 28.31610 28.16186 26.51348 26.02747
[49] 27.08449 21.91069
$x3
[1] 11.6201614 11.7248762 13.3374679 4.3256780 8.1032854 6.1235977
[7] 11.7805026 7.4148509 3.5650701 15.8647983 14.8382621 15.6786356
[13] 11.5860206 0.0198924 2.9176717 16.3342936 7.3713642 14.9845785
[19] 5.2901857 9.8382025 -2.2347269 16.1264894 16.4666958 7.4579946
[25] 4.4730358 14.7587509 4.5682974 9.8460002 0.3835374 18.2413788
[31] 7.5890881 0.8431373 16.4580390 14.2566923 6.0304444 6.5470628
[37] 22.3208480 5.2943139 12.7842887 1.5344631 6.7964196 -0.9742034
[43] 7.3987924 5.5863990 3.0760514 11.6147581 9.6301556 5.3435136
[49] 0.8412404 20.0820351
$x4
[1] 1.4423468 1.7144002 0.6814712 1.6969592 1.2746219 1.1918903 0.8507043
[8] 1.2746324 0.3850853 0.1225640 0.8898047 0.8581539 1.3253551 0.7939156
[15] 0.7914138 0.7263540 1.3253416 0.9616639 1.4976244 0.3633276 1.7613419
[22] 0.9318393 1.1681829 0.7034077 1.3640640 1.1932273 0.8706002 0.5524266
[29] 0.6099138 0.2450496 0.7283081 0.6691275 0.6675042 1.0578633 0.7729999
[36] 1.2503530 0.9114667 1.3385308 1.2386105 0.5828654 1.4406876 1.0419542
[43] 1.7966151 1.1035424 1.0303867 0.4414917 0.8689440 1.4014934 1.1922928
[50] 1.2072652
例2
List2<-list(y1=rpois(50,2),y2=rep(NA,50),y3=rpois(50,5),y4=rpois(50,2))输出结果List2
$y1[1] 0 1 2 2 2 2 3 2 0 5 0 3 1 2 1 6 1 3 3 0 3 2 0 2 1 1 4 4 2 1 1 3 0 0 2 2 3 1
[39] 1 1 4 1 2 0 1 1 1 0 2 1
$y2
[1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
[26] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
$y3
[1] 2 4 2 6 3 6 2 5 2 6 5 7 4 5 2 8 10 8 8 5 4 5 3 4 2
[26] 2 10 7 7 4 4 2 6 5 6 1 5 4 5 10 2 6 6 8 8 4 7 4 4 2
$y4
[1] 1 1 4 1 0 1 0 1 3 0 4 2 3 0 1 3 2 1 4 1 1 2 3 0 3 1 3 2 1 1 2 1 1 4 2 0 3 2
[39] 1 1 2 2 3 2 3 4 0 4 0 0
删除List2中仅包含NA的元素-
筛选器(Negate(anyNA),List2)
$y1[1] 0 1 2 2 2 2 3 2 0 5 0 3 1 2 1 6 1 3 3 0 3 2 0 2 1 1 4 4 2 1 1 3 0 0 2 2 3 1
[39] 1 1 4 1 2 0 1 1 1 0 2 1
$y3
[1] 2 4 2 6 3 6 2 5 2 6 5 7 4 5 2 8 10 8 8 5 4 5 3 4 2
[26] 2 10 7 7 4 4 2 6 5 6 1 5 4 5 10 2 6 6 8 8 4 7 4 4 2
$y4
[1] 1 1 4 1 0 1 0 1 3 0 4 2 3 0 1 3 2 1 4 1 1 2 3 0 3 1 3 2 1 1 2 1 1 4 2 0 3 2
[39] 1 1 2 2 3 2 3 4 0 4 0 0
以上是 如何删除仅在R中包含NA的列表元素? 的全部内容, 来源链接: utcz.com/z/355799.html