如何在 R 中按名称删除列表元素?
数据分析不仅包括数学运算,还要花费大量时间用于数据清理、数据挖掘、特征工程等。如果一项操作对实现目标没有帮助,可以考虑按名称删除列表元素。可以使用某个函数按名称删除元素。
示例
考虑以下列表
List1<- list(x1=rnorm(60,1,0.95),x2=rnorm(60,5424,127.68),x3=rnorm(60,50,2.35),x4=rnorm(60,20,3.24)) List1
输出
$x1 [1] -1.103107699 2.078722802 2.371164857 0.021881406 0.390992950 [6] 1.476385690 1.141207941 -0.087333092 0.782669645 -0.031383610 [11] 0.795189013 1.197991076 1.444175931 0.769249312 1.912066039 [16] 2.717922675 0.710198309 0.670194245 2.461528527 1.858932946 [21] 0.065507123 0.286159313 1.672737420 1.786018329 0.803019105 [26] 1.885696334 0.758169091 -0.410390929 -1.254737911 1.192054962 [31] 2.697837121 2.177011009 -1.459178547 2.733624037 1.271602761 [36] -0.566724365 0.006310726 0.350689440 1.144167032 2.300173244 [41] 0.806413286 0.508535201 1.581777888 -0.250193267 3.523530052 [46] 1.498029653 1.447637379 0.115807682 1.282445998 1.711245218 [51] 0.423081872 1.051167794 -0.265119562 -0.577337623 2.390421540 [56] 0.354019835 -1.883593673 2.598753510 0.351321430 -0.053119277 $x2 [1] 5324.288 5344.918 5241.660 5578.071 5329.440 5437.268 5351.409 5463.745 [9] 5535.259 5457.844 5640.661 5398.484 5383.392 5429.124 5357.249 5417.066 [17] 5234.672 5533.008 5555.331 5525.581 5419.722 5324.499 5495.273 5281.291 [25] 5570.078 5550.720 5449.779 5447.574 5579.832 5255.774 5475.710 5619.025 [33] 5570.046 5477.875 5495.976 5568.811 5247.030 5414.587 5190.818 5395.836 [41] 5500.447 5501.045 5314.469 5418.376 5535.070 5664.423 4935.960 5531.844 [49] 5378.684 5508.853 5522.674 5489.705 5512.495 5414.008 5505.148 5148.656 [57] 5586.520 5290.609 5397.421 5460.574 $x3 [1] 49.34596 51.32998 51.70226 49.68396 51.15468 50.26879 52.06086 48.99725 [9] 52.73262 48.60677 49.58647 51.88704 48.73080 48.13890 44.85399 46.20519 [17] 47.22128 50.60390 48.75666 48.84218 50.31721 50.67285 48.47646 46.34506 [25] 49.27422 49.62860 51.51841 45.83099 52.30268 53.66676 53.40591 48.06345 [33] 52.23169 51.96759 49.25928 52.12997 45.58793 50.01810 48.47573 48.46257 [41] 50.01778 52.13582 46.03818 48.50411 49.14832 51.11971 48.21913 49.86897 [49] 48.92824 51.70422 49.13782 47.28929 47.59428 54.85879 46.26745 50.44922 [57] 48.93762 49.45593 52.17696 49.34633 $x4 [1] 18.04474 23.41377 16.25905 16.28852 21.89753 25.99785 22.42163 21.69039 [9] 26.04570 18.27497 24.14191 19.02528 19.14901 22.03552 20.95017 14.05373 [17] 21.08831 22.05547 22.02216 26.58448 20.22041 18.25070 24.53673 23.53406 [25] 21.20194 26.09326 19.48507 27.61870 14.49611 22.72755 23.16411 19.92344 [33] 10.63012 12.80279 19.96753 15.59017 19.46262 21.70891 18.89588 21.09484 [41] 16.83731 17.37747 17.82628 20.00209 16.38927 16.54549 12.25639 23.48978 [49] 25.13921 14.30144 22.36671 23.48653 20.32195 22.40168 23.67391 10.55038 [57] 16.60622 22.41615 20.96008 18.15460
从 List1 中删除 x1 和 x2 -
示例
List1[which(names(List1)%in%c("x1","x2"))]<-NULL List1
输出
$x3 [1] 49.34596 51.32998 51.70226 49.68396 51.15468 50.26879 52.06086 48.99725 [9] 52.73262 48.60677 49.58647 51.88704 48.73080 48.13890 44.85399 46.20519 [17] 47.22128 50.60390 48.75666 48.84218 50.31721 50.67285 48.47646 46.34506 [25] 49.27422 49.62860 51.51841 45.83099 52.30268 53.66676 53.40591 48.06345 [33] 52.23169 51.96759 49.25928 52.12997 45.58793 50.01810 48.47573 48.46257 [41] 50.01778 52.13582 46.03818 48.50411 49.14832 51.11971 48.21913 49.86897 [49] 48.92824 51.70422 49.13782 47.28929 47.59428 54.85879 46.26745 50.44922 [57] 48.93762 49.45593 52.17696 49.34633 $x4 [1] 18.04474 23.41377 16.25905 16.28852 21.89753 25.99785 22.42163 21.69039 [9] 26.04570 18.27497 24.14191 19.02528 19.14901 22.03552 20.95017 14.05373 [17] 21.08831 22.05547 22.02216 26.58448 20.22041 18.25070 24.53673 23.53406 [25] 21.20194 26.09326 19.48507 27.61870 14.49611 22.72755 23.16411 19.92344 [33] 10.63012 12.80279 19.96753 15.59017 19.46262 21.70891 18.89588 21.09484 [41] 16.83731 17.37747 17.82628 20.00209 16.38927 16.54549 12.25639 23.48978 [49] 25.13921 14.30144 22.36671 23.48653 20.32195 22.40168 23.67391 10.55038 [57] 16.60622 22.41615 20.96008 18.15460
示例
List2<- list(y1=rpois(60,1),y2=rpois(60,5),y3=rpois(60,2),y4=rpois(60,3),y5=rpois(60,8)) List2
输出
$y1 [1] 0 1 2 1 0 1 2 2 1 1 1 0 0 1 0 0 1 1 1 0 1 0 0 3 1 0 2 1 2 0 1 1 2 1 1 1 0 1 [39] 0 4 1 1 1 0 0 0 1 1 0 1 1 2 1 1 0 1 1 1 2 3 $y2 [1] 3 6 4 3 3 4 4 5 1 3 6 3 4 4 5 2 6 8 3 5 5 3 6 5 7 [26] 5 7 3 4 8 3 5 7 5 4 6 9 4 2 6 8 4 6 6 1 7 10 5 6 3 [51] 3 5 4 0 6 4 7 6 8 7 $y3 [1] 3 2 2 4 2 0 3 0 1 1 2 4 0 2 3 1 3 1 3 3 2 1 2 2 4 1 1 3 1 1 1 2 2 4 3 1 2 2 [39] 1 1 1 0 3 6 2 1 3 1 2 4 1 2 1 0 3 5 2 3 0 2 $y4 [1] 3 1 4 4 3 4 3 2 1 3 6 1 4 2 1 2 2 1 3 1 3 2 2 1 0 4 3 5 3 3 2 0 2 2 2 3 1 3 [39] 2 4 4 2 1 2 3 3 5 0 2 1 8 4 1 5 3 3 2 3 4 6 $y5 [1] 8 7 6 14 6 14 9 3 11 12 11 6 11 3 8 4 8 5 11 6 4 7 7 5 11 [26] 7 5 6 8 4 9 3 6 9 7 3 10 3 9 13 9 6 14 5 14 10 5 7 6 4 [51] 3 13 5 11 7 9 5 5 5 4
从 List2 中删除 y1、y2 和 y3 -
示例
List2[which(names(List2)%in%c("y1","y2","y3"))]<-NULL List2
输出
$y4 [1] 3 1 4 4 3 4 3 2 1 3 6 1 4 2 1 2 2 1 3 1 3 2 2 1 0 4 3 5 3 3 2 0 2 2 2 3 1 3 [39] 2 4 4 2 1 2 3 3 5 0 2 1 8 4 1 5 3 3 2 3 4 6 $y5 [1] 8 7 6 14 6 14 9 3 11 12 11 6 11 3 8 4 8 5 11 6 4 7 7 5 11 [26] 7 5 6 8 4 9 3 6 9 7 3 10 3 9 13 9 6 14 5 14 10 5 7 6 4 [51] 3 13 5 11 7 9 5 5 5 4
广告