如何在R语言中提取列表所有元素的最后一个值?
要提取R语言列表中所有元素的最后一个值,我们可以按照以下步骤操作:
首先,创建一个列表。
然后,使用`tail`函数和`sapply`函数提取列表中所有元素的最后一个值。
示例1
创建列表
让我们创建一个如下所示的列表:
List<-list(x=rpois(200,5),y=rpois(200,2),z=rpois(200,10)) List
输出
执行上述脚本后,会生成以下输出(由于随机化,此输出可能因系统而异):
$x [1] 5 5 4 5 4 6 9 7 5 3 5 5 3 8 2 6 5 8 4 9 2 9 2 5 7 [26] 9 3 2 7 8 10 5 4 1 6 3 5 2 3 1 5 5 3 3 6 5 6 1 10 8 [51] 3 3 8 4 5 7 5 8 6 3 2 9 6 5 2 3 9 6 7 6 9 5 4 12 4 [76] 1 4 2 4 5 7 5 5 7 3 6 2 7 3 3 9 9 4 7 3 4 4 7 4 11 [101] 6 6 3 6 3 9 6 7 2 3 4 7 4 1 3 4 6 5 5 7 9 6 3 3 4 [126] 6 5 4 3 0 2 2 2 9 5 8 5 6 5 3 5 2 6 8 4 6 4 5 3 7 [151] 4 4 4 5 6 2 7 2 3 4 3 5 5 6 3 6 3 6 3 4 6 5 7 8 2 [176] 8 6 10 3 4 2 3 4 2 3 4 7 1 1 3 5 1 5 8 5 5 5 4 8 4 $y [1] 3 1 3 3 3 1 4 2 0 1 4 3 2 0 1 4 3 2 2 5 3 2 2 2 8 0 0 3 1 4 3 2 1 2 0 2 1 [38] 1 0 0 1 4 4 1 2 3 1 1 2 3 4 2 2 5 4 1 3 3 3 5 0 3 2 1 2 1 3 1 2 2 3 2 1 1 [75] 2 1 1 0 3 1 0 2 0 1 2 1 2 3 3 3 1 0 2 4 2 1 1 2 0 2 1 1 3 1 2 5 1 6 0 3 2 [112] 2 4 1 2 1 1 1 2 0 0 1 1 2 3 3 3 0 5 0 2 4 1 0 1 1 4 2 0 3 6 5 1 1 3 1 1 3 [149] 2 4 2 0 1 2 0 1 4 3 3 3 1 4 2 0 3 1 2 1 5 2 0 2 1 0 0 2 2 2 3 3 6 1 3 2 0 [186] 6 1 4 1 0 3 1 3 0 3 3 0 1 5 3 $z [1] 11 10 12 10 11 10 11 5 8 11 7 9 7 12 6 7 9 9 10 13 16 7 6 14 8 [26] 15 10 13 12 10 13 7 10 8 10 8 14 16 6 9 9 10 4 8 10 9 13 11 8 13 [51] 8 7 11 12 10 14 9 12 6 7 7 9 7 12 13 6 9 9 9 11 10 11 10 12 13 [76] 8 7 5 15 15 8 9 11 7 5 15 14 11 11 10 10 10 4 13 5 10 9 6 16 10 [101] 11 9 7 8 12 10 4 13 13 9 13 12 16 9 8 12 10 7 12 11 13 8 8 8 7 [126] 9 6 10 7 9 14 10 9 8 11 10 9 15 7 9 5 19 14 14 3 14 10 13 5 12 [151] 13 15 12 10 10 11 7 8 9 8 11 16 14 15 15 9 11 6 10 10 13 7 7 8 7 [176] 11 12 9 9 13 10 10 10 8 8 10 7 8 5 11 14 7 12 15 8 11 6 3 10 11
提取列表中所有元素的最后一个值
使用`tail`函数和`sapply`函数提取列表中的所有元素的最后一个值:
List<-list(x=rpois(200,5),y=rpois(200,2),z=rpois(200,10)) sapply(List,tail,1)
输出
x y z 4 5 4
示例2
创建列表
让我们创建一个如下所示的列表:
List_dat<-list(v1=rnorm(80),v2=rnorm(80),v3=rnorm(80),v4=rnorm(80)) List_dat
输出
执行上述脚本后,会生成以下输出(由于随机化,此输出可能因系统而异):
$v1 [1] -0.165619008 0.267304333 -0.311854408 -0.238362446 -1.061826764 [6] -0.902757798 -1.304989425 0.009363615 -0.493292919 0.565658904 [11] -2.198276663 -0.717800548 1.532979803 0.444892987 -1.419623758 [16] -0.502224305 -0.200736610 0.049186878 -0.441757302 -0.444764497 [21] -0.584759254 0.723140497 0.367873351 -0.180670874 -1.796277458 [26] 1.100384361 -1.458679536 0.219591257 -0.180696378 -0.330422342 [31] -1.553135827 -1.748224159 0.248185763 -0.009373961 1.125561626 [36] -0.982818712 0.969176592 1.565843138 0.715870619 0.103470665 [41] -1.502028543 -2.006461124 -0.354720099 -2.672053274 -2.816985415 [46] 0.206832137 -0.795527146 -0.760560831 -0.055845001 1.241985518 [51] -0.696766201 -0.057755913 -0.018895733 1.146811388 0.183040772 [56] 1.395248947 1.904267782 0.227063050 -0.994592435 -0.597812454 [61] -1.398883516 0.581127441 -0.132287974 0.140094678 0.687125139 [66] 0.943830156 1.356955760 0.484714531 -0.016157116 -0.316080153 [71] 0.017715555 -0.914927519 -0.544362899 -1.098048285 -0.221777322 [76] -0.702013912 0.713302291 0.415760032 0.165963414 -0.308774572 $v2 [1] 0.939470436 1.224423833 -0.678353206 0.256929871 -1.486556568 [6] 0.584678000 -1.939044925 0.589227763 0.466917601 0.979279670 [11] 1.215899592 0.171994303 0.341458646 1.247757791 -0.317927898 [16] -0.120414087 0.051772064 0.163214677 -1.183199180 -0.805426673 [21] 0.522099674 -1.312402258 -0.558474088 -0.965871568 -0.916720726 [26] 1.547692125 -2.241332630 1.494111931 -1.546344627 -0.897292897 [31] 0.453366769 0.154360059 -0.107195767 -0.996790344 0.053726718 [36] -1.182363178 -0.850161463 -0.583416507 1.335670572 0.974051746 [41] -0.529723025 0.915326717 0.635680306 -0.754858923 -0.565880529 [46] -1.035515553 -0.969044730 -0.958490706 -0.523441908 1.095373212 [51] 1.571221232 -0.466685502 0.383138894 -2.116778710 0.629212537 [56] 2.617438616 0.241360500 -2.834093313 0.373331037 1.050864716 [61] -2.617000251 1.371904324 0.022216534 0.902354826 0.828567910 [66] 1.337335128 0.443175631 0.698803995 1.615452305 -0.794854093 [71] -0.108666422 0.008559631 -1.156218790 -0.507252144 0.228189457 [76] 0.858232394 -0.874342697 -0.451921861 0.586591310 0.959680459 $v3 [1] -0.037753277 3.339534031 0.514752665 1.539406345 -1.895015064 [6] -0.533903863 0.972546150 0.050978469 2.221969338 1.617951459 [11] 0.980098124 -0.372557654 1.350539008 -0.471157353 -0.105476569 [16] -0.747279313 0.958785585 0.339965557 -0.164159199 -0.130173610 [21] 1.132918442 0.339870275 -1.926491430 0.522636467 0.716388988 [26] 0.679783801 1.392403414 -0.607788238 0.905557033 0.009501599 [31] 0.846922279 -2.261438842 -0.978736471 -0.802633182 1.429760151 [36] 0.298034357 0.030650462 -1.166376459 -1.060877246 0.313146937 [41] -0.433326714 -0.957050164 0.357296222 0.476765450 -0.629053646 [46] 0.669218564 -0.495977917 -0.648551863 0.184393330 0.317240459 [51] 0.132567947 1.401316320 0.221379860 0.062984921 -1.600515000 [56] 1.579340306 0.070115120 -0.181931429 -0.777450854 -0.954764769 [61] 0.206774487 0.930842296 -1.009157293 1.878353398 -0.691782997 [66] 1.107246619 -0.822550771 0.265348583 0.501030782 0.592028768 [71] -1.155336476 0.568988036 0.296063789 0.288713350 1.851737384 [76] -0.304182660 0.633713059 -0.633567625 -1.081723777 0.497455278 $v4 [1] -1.755908032 0.196341354 1.979263873 0.319594492 0.884962275 [6] 0.691062154 -1.060594939 -0.568229034 1.547051038 -1.418956106 [11] -0.859350736 -1.052428571 -1.023346185 0.765995100 -0.868273881 [16] 0.533401606 -0.914722544 -1.498923941 -1.197834007 0.956796938 [21] -0.023891162 -0.190712717 -1.446599652 0.599447419 0.345153505 [26] -0.777222622 -1.348610866 0.725954137 0.323154080 -0.461342070 [31] -0.171078041 -0.605117965 0.328708217 -0.457518147 0.008756334 [36] -0.073953767 -0.811685464 -0.453661714 1.746911365 -1.061391284 [41] -0.424500978 0.832235010 1.466961911 0.117079738 2.060399238 [46] -0.177322886 -0.989370627 -1.425955631 -0.015187413 -0.495382133 [51] -1.822225215 1.146436211 -0.619900870 0.405587917 1.846997193 [56] -0.886876238 0.630588180 -0.287299072 0.159368246 1.279635007 [61] 0.710200930 0.712856530 1.454045922 2.036560140 1.208812240 [66] -0.840375278 -1.075936233 0.079356105 0.578935542 -1.357121420 [71] 0.154157687 0.239540624 1.059662391 3.159911707 -1.207488171 [76] 1.918211043 -0.246651408 -0.253881664 -0.555600815 -0.546419848
提取列表中所有元素的最后一个值
使用`tail`函数和`sapply`函数提取List_dat中所有元素的最后一个值:
List_dat<-list(v1=rnorm(80),v2=rnorm(80),v3=rnorm(80),v4=rnorm(80)) sapply(List_dat,tail,1)
输出
v1 v2 v3 v4 -0.3087746 0.9596805 0.4974553 -0.5464198
广告