How to extract only first sub-element from a list in R?

R ProgrammingServer Side ProgrammingProgramming

To extract only first element from a list, we can use sapply function and access the first element with double square brackets. For example, if we have a list called LIST that contains 5 elements each containing 20 elements then the first sub-element can be extracted by using the command sapply(LIST,"[[",1).

Example1

Consider the below data frame −

 Live Demo

List1<-list(x1=rnorm(50),x2=rnorm(50),x3=rnorm(50),x4=rnorm(50))
List1

Output

$x1
[1] 0.161249858 0.036092622 -1.044116113 1.711548086 0.643501331
[6] 0.382859773 -0.792628167 -0.711300381 -0.422021222 0.745896168
[11] 0.196128414 -0.298657403 0.634259386 -0.437460026 -0.806850028
[16] 0.137813278 -0.006745093 -1.919094377 0.419949298 1.050802517
[21] -0.162697580 -0.182900434 0.911989574 0.456641498 0.501877946
[26] 1.019027122 -0.069249412 1.001133923 1.816262055 -1.047496717
[31] 0.287840421 0.742884790 -0.427961572 0.119784063 0.383153950
[36] -1.665170199 -0.524123898 0.286375036 -0.708250387 -0.715677729
[41] -1.894281105 -0.202630787 -1.925498062 -1.487425694 2.111946755
[46] 1.629015061 0.650948453 0.682603315 1.106431496 1.450096730
$x2
[1] 0.75378773 0.05249257 0.39375180 -0.19774168 0.39574154 0.21836481
[7] 0.46384754 -0.71917075 -1.43891164 -0.69293833 -0.45450458 -0.65776233
[13] 2.46455531 1.12250992 0.87239212 0.44463664 -0.78254250 -0.62857243
[19] 0.12313098 -0.70724790 0.59767273 -0.13321977 0.04811096 -0.54943951
[25] -0.28317900 0.27360876 0.32741112 -0.76000268 0.71394849 -0.52250184
[31] -0.36304544 0.46658600 0.12323175 -0.79092922 -1.18324766 -0.11712640
[37] 0.04891661 -0.24084771 0.61827617 -0.97107224 0.12126594 1.05355407
[43] -0.63041363 -0.78451193 0.53188055 -0.25393411 0.81460637 1.16082247
[49] -0.79304579 -0.97305990
$x3
[1] -0.13624214 1.90010480 0.69451332 2.13687511 -0.23066909 1.64692306
[7] -0.14711053 0.44208938 0.35531069 1.06209464 -1.81956846 -1.12509450
[13] 1.43256682 -0.02674276 -2.03427910 0.55615959 -1.40908574 -0.23011933
[19] -0.08360304 -0.50644360 -0.90820700 -0.24305283 -1.54668354 2.13954075
[25] 0.81472415 0.33037344 0.44158303 0.94220437 0.15045454 0.01357773
[31] 0.01231589 0.26622480 0.38282127 0.67098995 0.40259268 -1.44272053
[37] -0.70237660 0.39312657 -0.99737047 1.64467399 -0.91297020 -0.86162356
[43] -1.58048278 -0.24313534 -0.60451908 -0.64249832 -0.84198760 0.55292228
[49] 0.51750276 0.63131623
$x4
[1] -0.69509383 -0.22673120 0.48862526 0.51866187 -0.18995667 0.38839070
[7] 0.40172376 0.24034591 1.43575563 -0.96673760 1.90413695 2.37696269
[13] -0.67572738 2.03771152 -0.76142607 -0.66753431 -0.08930211 -2.32531986
[19] 0.42993078 -0.12933197 -1.58200214 1.60530348 0.59869548 0.68581561
[25] -0.22293216 -0.09693568 -0.14181344 -0.26035230 0.27148301 -0.12910718
[31] 0.20186925 0.62118418 -0.04921471 -0.61318088 -0.61041790 -0.91881398
[37] -0.31245844 0.71726962 1.12492512 0.89030799 -0.90499379 0.12365878
[43] -0.37452970 -0.54310263 0.46503125 -0.42180015 1.47912529 0.53569710
[49] 1.19356765 -0.04135349

Extracting first sub-element in List1 −

sapply(List1,"[[",1)

x1 x2 x3 x4
0.1612499 0.7537877 -0.1362421 -0.6950938

Example2

 Live Demo

List2<-list(y1=sample(LETTERS[1:4],50,replace=TRUE),y2=sample(LETTERS[1:4],50,replace=TRUE),y3=sample(LETTERS[1:4],50,replace=TRUE),y4=sample(LETTERS[1:4],50,replace=TRUE))
List2

Output

$y1
[1] "B" "C" "A" "C" "B" "D" "A" "C" "D" "B" "D" "C" "B" "D" "B" "A" "C" "A" "C"
[20] "C" "A" "D" "B" "C" "B" "B" "C" "D" "D" "D" "C" "B" "B" "B" "B" "B" "C" "D"
[39] "D" "C" "A" "D" "B" "C" "D" "D" "D" "C" "D" "C"
$y2
[1] "B" "A" "B" "C" "A" "B" "A" "A" "A" "C" "C" "A" "D" "A" "C" "B" "A" "A" "C"
[20] "A" "B" "B" "A" "A" "D" "D" "A" "D" "C" "C" "A" "D" "C" "A" "B" "C" "D" "A"
[39] "C" "D" "B" "A" "D" "C" "A" "C" "D" "A" "B" "A"
$y3
[1] "C" "B" "A" "D" "B" "B" "D" "A" "B" "A" "C" "B" "D" "B" "A" "B" "C" "D" "D"
[20] "B" "C" "D" "C" "D" "D" "D" "C" "B" "C" "D" "C" "D" "D" "C" "B" "C" "A" "D"
[39] "A" "A" "B" "B" "A" "C" "A" "D" "A" "D" "D" "A"
$y4
[1] "D" "D" "D" "C" "D" "B" "C" "B" "D" "D" "A" "D" "D" "D" "D" "B" "D" "C" "A"
[20] "D" "D" "D" "B" "D" "A" "C" "C" "D" "D" "D" "B" "B" "A" "D" "C" "C" "C" "D"
[39] "B" "D" "A" "C" "D" "D" "A" "B" "A" "D" "C" "B"

Extracting first sub-element in List2 −

sapply(List2,"[[",1)

y1 y2 y3 y4
"B" "B" "C" "D"
raja
Published on 06-Mar-2021 14:13:43
Advertisements